求教!如何修改程序,循环计算求值?
已知程序:#include <stdio.h>
main()
{
int m[7]={7,6,5,4,3,2,1};
int n[7]={22,6,5,4,3,2,1};
char a[7]={0};
int i,j, mc, nc;
char sm[256], sn[256];
for (i=0; i<7; i++)
{
for (j=1; j<=i; j++)
a[j] = 1;
while (1)
{
mc = m[0];
nc = n[0];
sprintf(sm, "%s", "m0");
sprintf(sn, "%s", "n0");
for (j=1; j<=i; j++)
{
if (a[j])
{
mc += m[j];
nc += n[j];
sprintf(sm, "%s+m%d", sm,j);
sprintf(sn, "%s+n%d", sn,j);
}
else
{
mc -= m[j];
nc -= n[j];
sprintf(sm, "%s-m%d", sm,j);
sprintf(sn, "%s-n%d", sn,j);
}
}
printf("%s/%s = ", sm,sn);
printf("%lf\n", (double)mc/nc);
for (j=i; j>0; j--)
{
if (a[j])
{
a[j] = 0;
break;
}
}
if (j == 0)
break;
for (++j; j<=i; j++)
a[j] = 1;
}
printf("\n===============================\n");
}
}
运行结果:
m0/n0 = 0.318182
===============================
m0+m1/n0+n1 = 0.464286
m0-m1/n0-n1 = 0.062500
===============================
m0+m1+m2/n0+n1+n2 = 0.545455
m0+m1-m2/n0+n1-n2 = 0.347826
m0-m1+m2/n0-n1+n2 = 0.285714
m0-m1-m2/n0-n1-n2 = -0.363636
===============================
m0+m1+m2+m3/n0+n1+n2+n3 = 0.594595
m0+m1+m2-m3/n0+n1+n2-n3 = 0.482759
m0+m1-m2+m3/n0+n1-n2+n3 = 0.444444
m0+m1-m2-m3/n0+n1-n2-n3 = 0.210526
m0-m1+m2+m3/n0-n1+n2+n3 = 0.400000
m0-m1+m2-m3/n0-n1+n2-n3 = 0.117647
m0-m1-m2+m3/n0-n1-n2+n3 = 0.000000
m0-m1-m2-m3/n0-n1-n2-n3 = -1.142857
===============================
m0+m1+m2+m3+m4/n0+n1+n2+n3+n4 = 0.625000
m0+m1+m2+m3-m4/n0+n1+n2+n3-n4 = 0.558824
m0+m1+m2-m3+m4/n0+n1+n2-n3+n4 = 0.531250
m0+m1+m2-m3-m4/n0+n1+n2-n3-n4 = 0.423077
m0+m1-m2+m3+m4/n0+n1-n2+n3+n4 = 0.500000
m0+m1-m2+m3-m4/n0+n1-n2+n3-n4 = 0.375000
m0+m1-m2-m3+m4/n0+n1-n2-n3+n4 = 0.318182
m0+m1-m2-m3-m4/n0+n1-n2-n3-n4 = 0.062500
m0-m1+m2+m3+m4/n0-n1+n2+n3+n4 = 0.464286
m0-m1+m2+m3-m4/n0-n1+n2+n3-n4 = 0.318182
m0-m1+m2-m3+m4/n0-n1+n2-n3+n4 = 0.250000
m0-m1+m2-m3-m4/n0-n1+n2-n3-n4 = -0.071429
m0-m1-m2+m3+m4/n0-n1-n2+n3+n4 = 0.166667
m0-m1-m2+m3-m4/n0-n1-n2+n3-n4 = -0.250000
m0-m1-m2-m3+m4/n0-n1-n2-n3+n4 = -0.500000
m0-m1-m2-m3-m4/n0-n1-n2-n3-n4 = -2.750000
===============================
m0+m1+m2+m3+m4+m5/n0+n1+n2+n3+n4+n5 = 0.642857
m0+m1+m2+m3+m4-m5/n0+n1+n2+n3+n4-n5 = 0.605263
m0+m1+m2+m3-m4+m5/n0+n1+n2+n3-n4+n5 = 0.583333
m0+m1+m2+m3-m4-m5/n0+n1+n2+n3-n4-n5 = 0.531250
m0+m1+m2-m3+m4+m5/n0+n1+n2-n3+n4+n5 = 0.558824
m0+m1+m2-m3+m4-m5/n0+n1+n2-n3+n4-n5 = 0.500000
m0+m1+m2-m3-m4+m5/n0+n1+n2-n3-n4+n5 = 0.464286
m0+m1+m2-m3-m4-m5/n0+n1+n2-n3-n4-n5 = 0.375000
m0+m1-m2+m3+m4+m5/n0+n1-n2+n3+n4+n5 = 0.531250
m0+m1-m2+m3+m4-m5/n0+n1-n2+n3+n4-n5 = 0.464286
m0+m1-m2+m3-m4+m5/n0+n1-n2+n3-n4+n5 = 0.423077
m0+m1-m2+m3-m4-m5/n0+n1-n2+n3-n4-n5 = 0.318182
m0+m1-m2-m3+m4+m5/n0+n1-n2-n3+n4+n5 = 0.375000
m0+m1-m2-m3+m4-m5/n0+n1-n2-n3+n4-n5 = 0.250000
m0+m1-m2-m3-m4+m5/n0+n1-n2-n3-n4+n5 = 0.166667
m0+m1-m2-m3-m4-m5/n0+n1-n2-n3-n4-n5 = -0.071429
m0-m1+m2+m3+m4+m5/n0-n1+n2+n3+n4+n5 = 0.500000
m0-m1+m2+m3+m4-m5/n0-n1+n2+n3+n4-n5 = 0.423077
m0-m1+m2+m3-m4+m5/n0-n1+n2+n3-n4+n5 = 0.375000
m0-m1+m2+m3-m4-m5/n0-n1+n2+n3-n4-n5 = 0.250000
m0-m1+m2-m3+m4+m5/n0-n1+n2-n3+n4+n5 = 0.318182
m0-m1+m2-m3+m4-m5/n0-n1+n2-n3+n4-n5 = 0.166667
m0-m1+m2-m3-m4+m5/n0-n1+n2-n3-n4+n5 = 0.062500
m0-m1+m2-m3-m4-m5/n0-n1+n2-n3-n4-n5 = -0.250000
m0-m1-m2+m3+m4+m5/n0-n1-n2+n3+n4+n5 = 0.250000
m0-m1-m2+m3+m4-m5/n0-n1-n2+n3+n4-n5 = 0.062500
m0-m1-m2+m3-m4+m5/n0-n1-n2+n3-n4+n5 = -0.071429
m0-m1-m2+m3-m4-m5/n0-n1-n2+n3-n4-n5 = -0.500000
m0-m1-m2-m3+m4+m5/n0-n1-n2-n3+n4+n5 = -0.250000
m0-m1-m2-m3+m4-m5/n0-n1-n2-n3+n4-n5 = -0.875000
m0-m1-m2-m3-m4+m5/n0-n1-n2-n3-n4+n5 = -1.500000
m0-m1-m2-m3-m4-m5/n0-n1-n2-n3-n4-n5 = -6.500000
===============================
m0+m1+m2+m3+m4+m5+m6/n0+n1+n2+n3+n4+n5+n6 = 0.651163
m0+m1+m2+m3+m4+m5-m6/n0+n1+n2+n3+n4+n5-n6 = 0.634146
m0+m1+m2+m3+m4-m5+m6/n0+n1+n2+n3+n4-n5+n6 = 0.615385
m0+m1+m2+m3+m4-m5-m6/n0+n1+n2+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/n0-n1+n2-n3-n4+n5-n6 = 0.000000
m0-m1+m2-m3-m4-m5+m6/n0-n1+n2-n3-n4-n5+n6 = -0.153846
m0-m1+m2-m3-m4-m5-m6/n0-n1+n2-n3-n4-n5-n6 = -0.363636
m0-m1-m2+m3+m4+m5+m6/n0-n1-n2+n3+n4+n5+n6 = 0.285714
m0-m1-m2+m3+m4+m5-m6/n0-n1-n2+n3+n4+n5-n6 = 0.210526
m0-m1-m2+m3+m4-m5+m6/n0-n1-n2+n3+n4-n5+n6 = 0.117647
m0-m1-m2+m3+m4-m5-m6/n0-n1-n2+n3+n4-n5-n6 = 0.000000
m0-m1-m2+m3-m4+m5+m6/n0-n1-n2+n3-n4+n5+n6 = 0.000000
m0-m1-m2+m3-m4+m5-m6/n0-n1-n2+n3-n4+n5-n6 = -0.153846
m0-m1-m2+m3-m4-m5+m6/n0-n1-n2+n3-n4-n5+n6 = -0.363636
m0-m1-m2+m3-m4-m5-m6/n0-n1-n2+n3-n4-n5-n6 = -0.666667
m0-m1-m2-m3+m4+m5+m6/n0-n1-n2-n3+n4+n5+n6 = -0.153846
m0-m1-m2-m3+m4+m5-m6/n0-n1-n2-n3+n4+n5-n6 = -0.363636
m0-m1-m2-m3+m4-m5+m6/n0-n1-n2-n3+n4-n5+n6 = -0.666667
m0-m1-m2-m3+m4-m5-m6/n0-n1-n2-n3+n4-n5-n6 = -1.142857
m0-m1-m2-m3-m4+m5+m6/n0-n1-n2-n3-n4+n5+n6 = -1.142857
m0-m1-m2-m3-m4+m5-m6/n0-n1-n2-n3-n4+n5-n6 = -2.000000
m0-m1-m2-m3-m4-m5+m6/n0-n1-n2-n3-n4-n5+n6 = -4.000000
m0-m1-m2-m3-m4-m5-m6/n0-n1-n2-n3-n4-n5-n6 = -14.000000
===============================
Press any key to continue
[此贴子已经被作者于2017-2-22 19:35编辑过]