| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 845 人关注过本帖
标题:求教!如何改进程序一小点,改变计算结果一大点?
只看楼主 加入收藏
宇宙规律
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:232
专家分:128
注 册:2014-5-7
结帖率:97.06%
收藏
已结贴  问题点数:80 回复次数:4 
求教!如何改进程序一小点,改变计算结果一大点?
已知程序:
#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, mmax,nmax;
    char sm[256], sn[256], smc[1024], snc[1024];
    printf("m[7]={7,-6,5,4,3,2,1}\n");
    printf("n[7]={22,-6,5,4,3,2,1}\n\n");
    for (i=0; i<7; i++)
    {
        for (j=1; j<=i; j++)
            a[j] = 1;
        mmax = m[0];
        nmax = n[0];
        while (1)
        {
            mc = m[0];
            nc = n[0];
            sprintf(sm, "%s", "m0");
            sprintf(sn, "%s", "n0");
            sprintf(smc, "%d", m[0]);
            sprintf(snc, "%d", n[0]);
            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);
                    sprintf(smc, "%s+%d", smc,m[j]);
                    sprintf(snc, "%s+%d", snc,n[j]);
                }
                else
                {
                    mc -= m[j];
                    nc -= n[j];
                    sprintf(sm, "%s-m%d", sm,j);
                    sprintf(sn, "%s-n%d", sn,j);
                    sprintf(smc, "%s-%d", smc,m[j]);
                    sprintf(snc, "%s-%d", snc,n[j]);
                }
            }
            if (mc > mmax)
            {
                mmax = mc;
                nmax = nc;
            }
            printf("(%s)/(%s) = ", sm,sn);
            printf("(%s)/(%s) = ", smc,snc);
            printf("%d/%d = ", mc,nc);
            if (nc != 0)
                printf("%lf\n", (double)mc/nc);
            else
                printf("∞\n");
            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;   
        }
        if (nmax != 0)
            printf("最大值%d/%d = %lf\n", mmax,nmax,(double)mmax/nmax);
        printf("\n===============================\n");
    }
}

运行结果:
m[7]={7,-6,5,4,3,2,1}
n[7]={22,-6,5,4,3,2,1}

(m0)/(n0) = (7)/(22) = 7/22 = 0.318182
最大值7/22 = 0.318182

===============================
(m0+m1)/(n0+n1) = (7+-6)/(22+-6) = 1/16 = 0.062500
(m0-m1)/(n0-n1) = (7--6)/(22--6) = 13/28 = 0.464286
最大值13/28 = 0.464286

===============================
(m0+m1+m2)/(n0+n1+n2) = (7+-6+5)/(22+-6+5) = 6/21 = 0.285714
(m0+m1-m2)/(n0+n1-n2) = (7+-6-5)/(22+-6-5) = -4/11 = -0.363636
(m0-m1+m2)/(n0-n1+n2) = (7--6+5)/(22--6+5) = 18/33 = 0.545455
(m0-m1-m2)/(n0-n1-n2) = (7--6-5)/(22--6-5) = 8/23 = 0.347826
最大值18/33 = 0.545455

===============================
(m0+m1+m2+m3)/(n0+n1+n2+n3) = (7+-6+5+4)/(22+-6+5+4) = 10/25 = 0.400000
(m0+m1+m2-m3)/(n0+n1+n2-n3) = (7+-6+5-4)/(22+-6+5-4) = 2/17 = 0.117647
(m0+m1-m2+m3)/(n0+n1-n2+n3) = (7+-6-5+4)/(22+-6-5+4) = 0/15 = 0.000000
(m0+m1-m2-m3)/(n0+n1-n2-n3) = (7+-6-5-4)/(22+-6-5-4) = -8/7 = -1.142857
(m0-m1+m2+m3)/(n0-n1+n2+n3) = (7--6+5+4)/(22--6+5+4) = 22/37 = 0.594595
(m0-m1+m2-m3)/(n0-n1+n2-n3) = (7--6+5-4)/(22--6+5-4) = 14/29 = 0.482759
(m0-m1-m2+m3)/(n0-n1-n2+n3) = (7--6-5+4)/(22--6-5+4) = 12/27 = 0.444444
(m0-m1-m2-m3)/(n0-n1-n2-n3) = (7--6-5-4)/(22--6-5-4) = 4/19 = 0.210526
最大值22/37 = 0.594595

===============================
(m0+m1+m2+m3+m4)/(n0+n1+n2+n3+n4) = (7+-6+5+4+3)/(22+-6+5+4+3) = 13/28 = 0.46428
6
(m0+m1+m2+m3-m4)/(n0+n1+n2+n3-n4) = (7+-6+5+4-3)/(22+-6+5+4-3) = 7/22 = 0.318182

(m0+m1+m2-m3+m4)/(n0+n1+n2-n3+n4) = (7+-6+5-4+3)/(22+-6+5-4+3) = 5/20 = 0.250000

(m0+m1+m2-m3-m4)/(n0+n1+n2-n3-n4) = (7+-6+5-4-3)/(22+-6+5-4-3) = -1/14 = -0.0714
29
(m0+m1-m2+m3+m4)/(n0+n1-n2+n3+n4) = (7+-6-5+4+3)/(22+-6-5+4+3) = 3/18 = 0.166667

(m0+m1-m2+m3-m4)/(n0+n1-n2+n3-n4) = (7+-6-5+4-3)/(22+-6-5+4-3) = -3/12 = -0.2500
00
(m0+m1-m2-m3+m4)/(n0+n1-n2-n3+n4) = (7+-6-5-4+3)/(22+-6-5-4+3) = -5/10 = -0.5000
00
(m0+m1-m2-m3-m4)/(n0+n1-n2-n3-n4) = (7+-6-5-4-3)/(22+-6-5-4-3) = -11/4 = -2.7500
00
(m0-m1+m2+m3+m4)/(n0-n1+n2+n3+n4) = (7--6+5+4+3)/(22--6+5+4+3) = 25/40 = 0.62500
0
(m0-m1+m2+m3-m4)/(n0-n1+n2+n3-n4) = (7--6+5+4-3)/(22--6+5+4-3) = 19/34 = 0.55882
4
(m0-m1+m2-m3+m4)/(n0-n1+n2-n3+n4) = (7--6+5-4+3)/(22--6+5-4+3) = 17/32 = 0.53125
0
(m0-m1+m2-m3-m4)/(n0-n1+n2-n3-n4) = (7--6+5-4-3)/(22--6+5-4-3) = 11/26 = 0.42307
7
(m0-m1-m2+m3+m4)/(n0-n1-n2+n3+n4) = (7--6-5+4+3)/(22--6-5+4+3) = 15/30 = 0.50000
0
(m0-m1-m2+m3-m4)/(n0-n1-n2+n3-n4) = (7--6-5+4-3)/(22--6-5+4-3) = 9/24 = 0.375000

(m0-m1-m2-m3+m4)/(n0-n1-n2-n3+n4) = (7--6-5-4+3)/(22--6-5-4+3) = 7/22 = 0.318182

(m0-m1-m2-m3-m4)/(n0-n1-n2-n3-n4) = (7--6-5-4-3)/(22--6-5-4-3) = 1/16 = 0.062500

最大值25/40 = 0.625000

===============================
(m0+m1+m2+m3+m4+m5)/(n0+n1+n2+n3+n4+n5) = (7+-6+5+4+3+2)/(22+-6+5+4+3+2) = 15/30
 = 0.500000
(m0+m1+m2+m3+m4-m5)/(n0+n1+n2+n3+n4-n5) = (7+-6+5+4+3-2)/(22+-6+5+4+3-2) = 11/26
 = 0.423077
(m0+m1+m2+m3-m4+m5)/(n0+n1+n2+n3-n4+n5) = (7+-6+5+4-3+2)/(22+-6+5+4-3+2) = 9/24
= 0.375000
(m0+m1+m2+m3-m4-m5)/(n0+n1+n2+n3-n4-n5) = (7+-6+5+4-3-2)/(22+-6+5+4-3-2) = 5/20
= 0.250000
(m0+m1+m2-m3+m4+m5)/(n0+n1+n2-n3+n4+n5) = (7+-6+5-4+3+2)/(22+-6+5-4+3+2) = 7/22
= 0.318182
(m0+m1+m2-m3+m4-m5)/(n0+n1+n2-n3+n4-n5) = (7+-6+5-4+3-2)/(22+-6+5-4+3-2) = 3/18
= 0.166667
(m0+m1+m2-m3-m4+m5)/(n0+n1+n2-n3-n4+n5) = (7+-6+5-4-3+2)/(22+-6+5-4-3+2) = 1/16
= 0.062500
(m0+m1+m2-m3-m4-m5)/(n0+n1+n2-n3-n4-n5) = (7+-6+5-4-3-2)/(22+-6+5-4-3-2) = -3/12
 = -0.250000
(m0+m1-m2+m3+m4+m5)/(n0+n1-n2+n3+n4+n5) = (7+-6-5+4+3+2)/(22+-6-5+4+3+2) = 5/20
= 0.250000
(m0+m1-m2+m3+m4-m5)/(n0+n1-n2+n3+n4-n5) = (7+-6-5+4+3-2)/(22+-6-5+4+3-2) = 1/16
= 0.062500
(m0+m1-m2+m3-m4+m5)/(n0+n1-n2+n3-n4+n5) = (7+-6-5+4-3+2)/(22+-6-5+4-3+2) = -1/14
 = -0.071429
(m0+m1-m2+m3-m4-m5)/(n0+n1-n2+n3-n4-n5) = (7+-6-5+4-3-2)/(22+-6-5+4-3-2) = -5/10
 = -0.500000
(m0+m1-m2-m3+m4+m5)/(n0+n1-n2-n3+n4+n5) = (7+-6-5-4+3+2)/(22+-6-5-4+3+2) = -3/12
 = -0.250000
(m0+m1-m2-m3+m4-m5)/(n0+n1-n2-n3+n4-n5) = (7+-6-5-4+3-2)/(22+-6-5-4+3-2) = -7/8
= -0.875000
(m0+m1-m2-m3-m4+m5)/(n0+n1-n2-n3-n4+n5) = (7+-6-5-4-3+2)/(22+-6-5-4-3+2) = -9/6
= -1.500000
(m0+m1-m2-m3-m4-m5)/(n0+n1-n2-n3-n4-n5) = (7+-6-5-4-3-2)/(22+-6-5-4-3-2) = -13/2
 = -6.500000
(m0-m1+m2+m3+m4+m5)/(n0-n1+n2+n3+n4+n5) = (7--6+5+4+3+2)/(22--6+5+4+3+2) = 27/42
 = 0.642857
(m0-m1+m2+m3+m4-m5)/(n0-n1+n2+n3+n4-n5) = (7--6+5+4+3-2)/(22--6+5+4+3-2) = 23/38
 = 0.605263
(m0-m1+m2+m3-m4+m5)/(n0-n1+n2+n3-n4+n5) = (7--6+5+4-3+2)/(22--6+5+4-3+2) = 21/36
 = 0.583333
(m0-m1+m2+m3-m4-m5)/(n0-n1+n2+n3-n4-n5) = (7--6+5+4-3-2)/(22--6+5+4-3-2) = 17/32
 = 0.531250
(m0-m1+m2-m3+m4+m5)/(n0-n1+n2-n3+n4+n5) = (7--6+5-4+3+2)/(22--6+5-4+3+2) = 19/34
 = 0.558824
(m0-m1+m2-m3+m4-m5)/(n0-n1+n2-n3+n4-n5) = (7--6+5-4+3-2)/(22--6+5-4+3-2) = 15/30
 = 0.500000
(m0-m1+m2-m3-m4+m5)/(n0-n1+n2-n3-n4+n5) = (7--6+5-4-3+2)/(22--6+5-4-3+2) = 13/28
 = 0.464286
(m0-m1+m2-m3-m4-m5)/(n0-n1+n2-n3-n4-n5) = (7--6+5-4-3-2)/(22--6+5-4-3-2) = 9/24
= 0.375000
(m0-m1-m2+m3+m4+m5)/(n0-n1-n2+n3+n4+n5) = (7--6-5+4+3+2)/(22--6-5+4+3+2) = 17/32
 = 0.531250
(m0-m1-m2+m3+m4-m5)/(n0-n1-n2+n3+n4-n5) = (7--6-5+4+3-2)/(22--6-5+4+3-2) = 13/28
 = 0.464286
(m0-m1-m2+m3-m4+m5)/(n0-n1-n2+n3-n4+n5) = (7--6-5+4-3+2)/(22--6-5+4-3+2) = 11/26
 = 0.423077
(m0-m1-m2+m3-m4-m5)/(n0-n1-n2+n3-n4-n5) = (7--6-5+4-3-2)/(22--6-5+4-3-2) = 7/22
= 0.318182
(m0-m1-m2-m3+m4+m5)/(n0-n1-n2-n3+n4+n5) = (7--6-5-4+3+2)/(22--6-5-4+3+2) = 9/24
= 0.375000
(m0-m1-m2-m3+m4-m5)/(n0-n1-n2-n3+n4-n5) = (7--6-5-4+3-2)/(22--6-5-4+3-2) = 5/20
= 0.250000
(m0-m1-m2-m3-m4+m5)/(n0-n1-n2-n3-n4+n5) = (7--6-5-4-3+2)/(22--6-5-4-3+2) = 3/18
= 0.166667
(m0-m1-m2-m3-m4-m5)/(n0-n1-n2-n3-n4-n5) = (7--6-5-4-3-2)/(22--6-5-4-3-2) = -1/14
 = -0.071429
最大值27/42 = 0.642857

===============================
(m0+m1+m2+m3+m4+m5+m6)/(n0+n1+n2+n3+n4+n5+n6) = (7+-6+5+4+3+2+1)/(22+-6+5+4+3+2+
1) = 16/31 = 0.516129
(m0+m1+m2+m3+m4+m5-m6)/(n0+n1+n2+n3+n4+n5-n6) = (7+-6+5+4+3+2-1)/(22+-6+5+4+3+2-
1) = 14/29 = 0.482759
(m0+m1+m2+m3+m4-m5+m6)/(n0+n1+n2+n3+n4-n5+n6) = (7+-6+5+4+3-2+1)/(22+-6+5+4+3-2+
1) = 12/27 = 0.444444
(m0+m1+m2+m3+m4-m5-m6)/(n0+n1+n2+n3+n4-n5-n6) = (7+-6+5+4+3-2-1)/(22+-6+5+4+3-2-
1) = 10/25 = 0.400000
(m0+m1+m2+m3-m4+m5+m6)/(n0+n1+n2+n3-n4+n5+n6) = (7+-6+5+4-3+2+1)/(22+-6+5+4-3+2+
1) = 10/25 = 0.400000
(m0+m1+m2+m3-m4+m5-m6)/(n0+n1+n2+n3-n4+n5-n6) = (7+-6+5+4-3+2-1)/(22+-6+5+4-3+2-
1) = 8/23 = 0.347826
(m0+m1+m2+m3-m4-m5+m6)/(n0+n1+n2+n3-n4-n5+n6) = (7+-6+5+4-3-2+1)/(22+-6+5+4-3-2+
1) = 6/21 = 0.285714
(m0+m1+m2+m3-m4-m5-m6)/(n0+n1+n2+n3-n4-n5-n6) = (7+-6+5+4-3-2-1)/(22+-6+5+4-3-2-
1) = 4/19 = 0.210526
(m0+m1+m2-m3+m4+m5+m6)/(n0+n1+n2-n3+n4+n5+n6) = (7+-6+5-4+3+2+1)/(22+-6+5-4+3+2+
1) = 8/23 = 0.347826
(m0+m1+m2-m3+m4+m5-m6)/(n0+n1+n2-n3+n4+n5-n6) = (7+-6+5-4+3+2-1)/(22+-6+5-4+3+2-
1) = 6/21 = 0.285714
(m0+m1+m2-m3+m4-m5+m6)/(n0+n1+n2-n3+n4-n5+n6) = (7+-6+5-4+3-2+1)/(22+-6+5-4+3-2+
1) = 4/19 = 0.210526
(m0+m1+m2-m3+m4-m5-m6)/(n0+n1+n2-n3+n4-n5-n6) = (7+-6+5-4+3-2-1)/(22+-6+5-4+3-2-
1) = 2/17 = 0.117647
(m0+m1+m2-m3-m4+m5+m6)/(n0+n1+n2-n3-n4+n5+n6) = (7+-6+5-4-3+2+1)/(22+-6+5-4-3+2+
1) = 2/17 = 0.117647
(m0+m1+m2-m3-m4+m5-m6)/(n0+n1+n2-n3-n4+n5-n6) = (7+-6+5-4-3+2-1)/(22+-6+5-4-3+2-
1) = 0/15 = 0.000000
(m0+m1+m2-m3-m4-m5+m6)/(n0+n1+n2-n3-n4-n5+n6) = (7+-6+5-4-3-2+1)/(22+-6+5-4-3-2+
1) = -2/13 = -0.153846
(m0+m1+m2-m3-m4-m5-m6)/(n0+n1+n2-n3-n4-n5-n6) = (7+-6+5-4-3-2-1)/(22+-6+5-4-3-2-
1) = -4/11 = -0.363636
(m0+m1-m2+m3+m4+m5+m6)/(n0+n1-n2+n3+n4+n5+n6) = (7+-6-5+4+3+2+1)/(22+-6-5+4+3+2+
1) = 6/21 = 0.285714
(m0+m1-m2+m3+m4+m5-m6)/(n0+n1-n2+n3+n4+n5-n6) = (7+-6-5+4+3+2-1)/(22+-6-5+4+3+2-
1) = 4/19 = 0.210526
(m0+m1-m2+m3+m4-m5+m6)/(n0+n1-n2+n3+n4-n5+n6) = (7+-6-5+4+3-2+1)/(22+-6-5+4+3-2+
1) = 2/17 = 0.117647
(m0+m1-m2+m3+m4-m5-m6)/(n0+n1-n2+n3+n4-n5-n6) = (7+-6-5+4+3-2-1)/(22+-6-5+4+3-2-
1) = 0/15 = 0.000000
(m0+m1-m2+m3-m4+m5+m6)/(n0+n1-n2+n3-n4+n5+n6) = (7+-6-5+4-3+2+1)/(22+-6-5+4-3+2+
1) = 0/15 = 0.000000
(m0+m1-m2+m3-m4+m5-m6)/(n0+n1-n2+n3-n4+n5-n6) = (7+-6-5+4-3+2-1)/(22+-6-5+4-3+2-
1) = -2/13 = -0.153846
(m0+m1-m2+m3-m4-m5+m6)/(n0+n1-n2+n3-n4-n5+n6) = (7+-6-5+4-3-2+1)/(22+-6-5+4-3-2+
1) = -4/11 = -0.363636
(m0+m1-m2+m3-m4-m5-m6)/(n0+n1-n2+n3-n4-n5-n6) = (7+-6-5+4-3-2-1)/(22+-6-5+4-3-2-
1) = -6/9 = -0.666667
(m0+m1-m2-m3+m4+m5+m6)/(n0+n1-n2-n3+n4+n5+n6) = (7+-6-5-4+3+2+1)/(22+-6-5-4+3+2+
1) = -2/13 = -0.153846
(m0+m1-m2-m3+m4+m5-m6)/(n0+n1-n2-n3+n4+n5-n6) = (7+-6-5-4+3+2-1)/(22+-6-5-4+3+2-
1) = -4/11 = -0.363636
(m0+m1-m2-m3+m4-m5+m6)/(n0+n1-n2-n3+n4-n5+n6) = (7+-6-5-4+3-2+1)/(22+-6-5-4+3-2+
1) = -6/9 = -0.666667
(m0+m1-m2-m3+m4-m5-m6)/(n0+n1-n2-n3+n4-n5-n6) = (7+-6-5-4+3-2-1)/(22+-6-5-4+3-2-
1) = -8/7 = -1.142857
(m0+m1-m2-m3-m4+m5+m6)/(n0+n1-n2-n3-n4+n5+n6) = (7+-6-5-4-3+2+1)/(22+-6-5-4-3+2+
1) = -8/7 = -1.142857
(m0+m1-m2-m3-m4+m5-m6)/(n0+n1-n2-n3-n4+n5-n6) = (7+-6-5-4-3+2-1)/(22+-6-5-4-3+2-
1) = -10/5 = -2.000000
(m0+m1-m2-m3-m4-m5+m6)/(n0+n1-n2-n3-n4-n5+n6) = (7+-6-5-4-3-2+1)/(22+-6-5-4-3-2+
1) = -12/3 = -4.000000
(m0+m1-m2-m3-m4-m5-m6)/(n0+n1-n2-n3-n4-n5-n6) = (7+-6-5-4-3-2-1)/(22+-6-5-4-3-2-
1) = -14/1 = -14.000000
(m0-m1+m2+m3+m4+m5+m6)/(n0-n1+n2+n3+n4+n5+n6) = (7--6+5+4+3+2+1)/(22--6+5+4+3+2+
1) = 28/43 = 0.651163
(m0-m1+m2+m3+m4+m5-m6)/(n0-n1+n2+n3+n4+n5-n6) = (7--6+5+4+3+2-1)/(22--6+5+4+3+2-
1) = 26/41 = 0.634146
(m0-m1+m2+m3+m4-m5+m6)/(n0-n1+n2+n3+n4-n5+n6) = (7--6+5+4+3-2+1)/(22--6+5+4+3-2+
1) = 24/39 = 0.615385
(m0-m1+m2+m3+m4-m5-m6)/(n0-n1+n2+n3+n4-n5-n6) = (7--6+5+4+3-2-1)/(22--6+5+4+3-2-
1) = 22/37 = 0.594595
(m0-m1+m2+m3-m4+m5+m6)/(n0-n1+n2+n3-n4+n5+n6) = (7--6+5+4-3+2+1)/(22--6+5+4-3+2+
1) = 22/37 = 0.594595
(m0-m1+m2+m3-m4+m5-m6)/(n0-n1+n2+n3-n4+n5-n6) = (7--6+5+4-3+2-1)/(22--6+5+4-3+2-
1) = 20/35 = 0.571429
(m0-m1+m2+m3-m4-m5+m6)/(n0-n1+n2+n3-n4-n5+n6) = (7--6+5+4-3-2+1)/(22--6+5+4-3-2+
1) = 18/33 = 0.545455
(m0-m1+m2+m3-m4-m5-m6)/(n0-n1+n2+n3-n4-n5-n6) = (7--6+5+4-3-2-1)/(22--6+5+4-3-2-
1) = 16/31 = 0.516129
(m0-m1+m2-m3+m4+m5+m6)/(n0-n1+n2-n3+n4+n5+n6) = (7--6+5-4+3+2+1)/(22--6+5-4+3+2+
1) = 20/35 = 0.571429
(m0-m1+m2-m3+m4+m5-m6)/(n0-n1+n2-n3+n4+n5-n6) = (7--6+5-4+3+2-1)/(22--6+5-4+3+2-
1) = 18/33 = 0.545455
(m0-m1+m2-m3+m4-m5+m6)/(n0-n1+n2-n3+n4-n5+n6) = (7--6+5-4+3-2+1)/(22--6+5-4+3-2+
1) = 16/31 = 0.516129
(m0-m1+m2-m3+m4-m5-m6)/(n0-n1+n2-n3+n4-n5-n6) = (7--6+5-4+3-2-1)/(22--6+5-4+3-2-
1) = 14/29 = 0.482759
(m0-m1+m2-m3-m4+m5+m6)/(n0-n1+n2-n3-n4+n5+n6) = (7--6+5-4-3+2+1)/(22--6+5-4-3+2+
1) = 14/29 = 0.482759
(m0-m1+m2-m3-m4+m5-m6)/(n0-n1+n2-n3-n4+n5-n6) = (7--6+5-4-3+2-1)/(22--6+5-4-3+2-
1) = 12/27 = 0.444444
(m0-m1+m2-m3-m4-m5+m6)/(n0-n1+n2-n3-n4-n5+n6) = (7--6+5-4-3-2+1)/(22--6+5-4-3-2+
1) = 10/25 = 0.400000
(m0-m1+m2-m3-m4-m5-m6)/(n0-n1+n2-n3-n4-n5-n6) = (7--6+5-4-3-2-1)/(22--6+5-4-3-2-
1) = 8/23 = 0.347826
(m0-m1-m2+m3+m4+m5+m6)/(n0-n1-n2+n3+n4+n5+n6) = (7--6-5+4+3+2+1)/(22--6-5+4+3+2+
1) = 18/33 = 0.545455
(m0-m1-m2+m3+m4+m5-m6)/(n0-n1-n2+n3+n4+n5-n6) = (7--6-5+4+3+2-1)/(22--6-5+4+3+2-
1) = 16/31 = 0.516129
(m0-m1-m2+m3+m4-m5+m6)/(n0-n1-n2+n3+n4-n5+n6) = (7--6-5+4+3-2+1)/(22--6-5+4+3-2+
1) = 14/29 = 0.482759
(m0-m1-m2+m3+m4-m5-m6)/(n0-n1-n2+n3+n4-n5-n6) = (7--6-5+4+3-2-1)/(22--6-5+4+3-2-
1) = 12/27 = 0.444444
(m0-m1-m2+m3-m4+m5+m6)/(n0-n1-n2+n3-n4+n5+n6) = (7--6-5+4-3+2+1)/(22--6-5+4-3+2+
1) = 12/27 = 0.444444
(m0-m1-m2+m3-m4+m5-m6)/(n0-n1-n2+n3-n4+n5-n6) = (7--6-5+4-3+2-1)/(22--6-5+4-3+2-
1) = 10/25 = 0.400000
(m0-m1-m2+m3-m4-m5+m6)/(n0-n1-n2+n3-n4-n5+n6) = (7--6-5+4-3-2+1)/(22--6-5+4-3-2+
1) = 8/23 = 0.347826
(m0-m1-m2+m3-m4-m5-m6)/(n0-n1-n2+n3-n4-n5-n6) = (7--6-5+4-3-2-1)/(22--6-5+4-3-2-
1) = 6/21 = 0.285714
(m0-m1-m2-m3+m4+m5+m6)/(n0-n1-n2-n3+n4+n5+n6) = (7--6-5-4+3+2+1)/(22--6-5-4+3+2+
1) = 10/25 = 0.400000
(m0-m1-m2-m3+m4+m5-m6)/(n0-n1-n2-n3+n4+n5-n6) = (7--6-5-4+3+2-1)/(22--6-5-4+3+2-
1) = 8/23 = 0.347826
(m0-m1-m2-m3+m4-m5+m6)/(n0-n1-n2-n3+n4-n5+n6) = (7--6-5-4+3-2+1)/(22--6-5-4+3-2+
1) = 6/21 = 0.285714
(m0-m1-m2-m3+m4-m5-m6)/(n0-n1-n2-n3+n4-n5-n6) = (7--6-5-4+3-2-1)/(22--6-5-4+3-2-
1) = 4/19 = 0.210526
(m0-m1-m2-m3-m4+m5+m6)/(n0-n1-n2-n3-n4+n5+n6) = (7--6-5-4-3+2+1)/(22--6-5-4-3+2+
1) = 4/19 = 0.210526
(m0-m1-m2-m3-m4+m5-m6)/(n0-n1-n2-n3-n4+n5-n6) = (7--6-5-4-3+2-1)/(22--6-5-4-3+2-
1) = 2/17 = 0.117647
(m0-m1-m2-m3-m4-m5+m6)/(n0-n1-n2-n3-n4-n5+n6) = (7--6-5-4-3-2+1)/(22--6-5-4-3-2+
1) = 0/15 = 0.000000
(m0-m1-m2-m3-m4-m5-m6)/(n0-n1-n2-n3-n4-n5-n6) = (7--6-5-4-3-2-1)/(22--6-5-4-3-2-
1) = -2/13 = -0.153846
最大值28/43 = 0.651163

===============================
Press any key to continue

如何改进程序,计算出:
m[7]={7,-6,5,4,3,2,1}
n[7]={22,-6,5,4,3,2,1}

1:计算:排序找出m[0],m[1],m[2],m[3],m[4],m[5],m[6],中的最大值;
   输出:(当m[i]是最大值,且n[i]!=0时),m[i]/n[i];    (例如:7最大,22不等于0,输出7/22=0.318182;)

2:计算:排序找出m[0]+m[1],m[1]+m[2],m[2]+m[3],m[3]+m[4],m[4]+m[5],m[5]+m[6],中的最大值;
   输出:(当m[i]+m[i+1]是最大值,且n[i]+n[i+1]!=0时),(m[i]+m[i+1])/(n[i]+n[i+1]);
   计算:排序找出m[0]-m[1],m[1]-m[2],m[2]-m[3],m[3]-m[4],m[4]-m[5],m[5]-m[6],中的最大值;
   输出:(当m[i]-m[i+1]是最大值,且n[i]-n[i+1]!=0时),(m[i]-m[i+1])/(n[i]-n[i+1]);

3:

4:

5:

6:

7:


  




[此贴子已经被作者于2017-2-23 10:30编辑过]

搜索更多相关主题的帖子: 如何 include 
2017-02-23 10:29
宇宙规律
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:232
专家分:128
注 册:2014-5-7
收藏
得分:0 
简化程序:
#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, mmax,nmax;
   
    for (i=0; i<7; i++)
    {
        for (j=1; j<=i; j++)
            a[j] = 1;
        mmax = m[0];
        nmax = n[0];
        while (1)
        {
            mc = m[0];
            nc = n[0];
           
            for (j=1; j<=i; j++)
            {
                if (a[j])
                {
                    mc += m[j];
                    nc += n[j];
                  
                }
                else
                {
                    mc -= m[j];
                    nc -= n[j];
                    
                }
            }
            if (mc > mmax)
            {
                mmax = mc;
                nmax = nc;
            }
            
           
            if (nc != 0)
                printf("%lf\n", (double)mc/nc);
            else
                printf("∞\n");
            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;   
        }
        if (nmax != 0)
            printf("最大值%d/%d = %lf\n", mmax,nmax,(double)mmax/nmax);
        printf("\n===============================\n");
    }
}

0.318182
最大值7/22 = 0.318182

===============================
0.062500
0.464286
最大值13/28 = 0.464286

===============================
0.285714
-0.363636
0.545455
0.347826
最大值18/33 = 0.545455

===============================
0.400000
0.117647
0.000000
-1.142857
0.594595
0.482759
0.444444
0.210526
最大值22/37 = 0.594595

===============================
0.464286
0.318182
0.250000
-0.071429
0.166667
-0.250000
-0.500000
-2.750000
0.625000
0.558824
0.531250
0.423077
0.500000
0.375000
0.318182
0.062500
最大值25/40 = 0.625000

===============================
0.500000
0.423077
0.375000
0.250000
0.318182
0.166667
0.062500
-0.250000
0.250000
0.062500
-0.071429
-0.500000
-0.250000
-0.875000
-1.500000
-6.500000
0.642857
0.605263
0.583333
0.531250
0.558824
0.500000
0.464286
0.375000
0.531250
0.464286
0.423077
0.318182
0.375000
0.250000
0.166667
-0.071429
最大值27/42 = 0.642857

===============================
0.516129
0.482759
0.444444
0.400000
0.400000
0.347826
0.285714
0.210526
0.347826
0.285714
0.210526
0.117647
0.117647
0.000000
-0.153846
-0.363636
0.285714
0.210526
0.117647
0.000000
0.000000
-0.153846
-0.363636
-0.666667
-0.153846
-0.363636
-0.666667
-1.142857
-1.142857
-2.000000
-4.000000
-14.000000
0.651163
0.634146
0.615385
0.594595
0.594595
0.571429
0.545455
0.516129
0.571429
0.545455
0.516129
0.482759
0.482759
0.444444
0.400000
0.347826
0.545455
0.516129
0.482759
0.444444
0.444444
0.400000
0.347826
0.285714
0.400000
0.347826
0.285714
0.210526
0.210526
0.117647
0.000000
-0.153846
最大值28/43 = 0.651163

===============================
Press any key to continue
2017-02-23 13:45
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10607
专家分:43186
注 册:2014-5-20
收藏
得分:80 
改了一下,没测算,不知有无理解错。
图片附件: 游客没有浏览图片的权限,请 登录注册

#include <stdio.h>
main()
{
    int m[7]={7,-6,5,4,3,2,1};
    int n[7]={22,-6,5,4,3,2,1};
    int i,j, k;
    int madd, msub, maddmax, msubmax;
    int nadd, nsub, naddmax, nsubmax;
    char sadd[256], ssub[256];
    printf("m[7]={7,-6,5,4,3,2,1}\n");
    printf("n[7]={22,-6,5,4,3,2,1}\n\n");
    for (i=0; i<7; i++)
    {
        maddmax = 0x80000000;
        msubmax = 0x80000000;
        naddmax = 0x80000000;
        nsubmax = 0x80000000;
        for (j=0; j<7-i; j++)
        {
            sprintf(sadd, "%d", m[j]);
            sprintf(ssub, "%d", m[j]);
            madd = m[j];
            msub = m[j];
            nadd = n[j];
            nsub = n[j];
            for (k=j+1; k<=j+i; k++)
            {
                sprintf(sadd, "%s + %d", sadd,m[k]);
                sprintf(ssub, "%s - %d", ssub,m[k]);
                madd += m[k];
                msub -= m[k];
                nadd += n[k];
                nsub -= n[k];
            }
            printf("%s = %d\n", sadd, madd);
            printf("%s = %d\n", ssub, msub);
            
            if (madd > maddmax)
            {
                maddmax = madd;
                naddmax = nadd;
            }
            if (msub > msubmax)
            {
                msubmax = msub;
                nsubmax = nsub;
            }
        }
        if (naddmax != 0)
            printf("+最大值 %d/%d=%lf\n", maddmax,naddmax,(double)maddmax/naddmax);
        if (nsubmax != 0)
            printf("-最大值 %d/%d=%lf\n", msubmax,nsubmax,(double)msubmax/nsubmax);
        printf("==================================\n");
    }
}
2017-02-23 16:21
宇宙规律
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:232
专家分:128
注 册:2014-5-7
收藏
得分:0 

感谢指教!您写的程序非常好,完美解答了我的计算编程难题!和心有善愿危急时刻,伸出援手救助落难落水的人一样,功德无量!

而这个被救的人,也是对社会未来会很有作用的某一个人;


m[7]={7,-6,5,4,3,2,1}
n[7]={22,-6,5,4,3,2,1}

7 = 7
7 = 7
-6 = -6
-6 = -6
5 = 5
5 = 5
4 = 4
4 = 4
3 = 3
3 = 3
2 = 2
2 = 2
1 = 1
1 = 1
+最大值 7/22=0.318182
-最大值 7/22=0.318182
==================================
7 + -6 = 1
7 - -6 = 13
-6 + 5 = -1
-6 - 5 = -11
5 + 4 = 9
5 - 4 = 1
4 + 3 = 7
4 - 3 = 1
3 + 2 = 5
3 - 2 = 1
2 + 1 = 3
2 - 1 = 1
+最大值 9/9=1.000000
-最大值 13/28=0.464286
==================================
7 + -6 + 5 = 6
7 - -6 - 5 = 8
-6 + 5 + 4 = 3
-6 - 5 - 4 = -15
5 + 4 + 3 = 12
5 - 4 - 3 = -2
4 + 3 + 2 = 9
4 - 3 - 2 = -1
3 + 2 + 1 = 6
3 - 2 - 1 = 0
+最大值 12/12=1.000000
-最大值 8/23=0.347826
==================================
7 + -6 + 5 + 4 = 10
7 - -6 - 5 - 4 = 4
-6 + 5 + 4 + 3 = 6
-6 - 5 - 4 - 3 = -18
5 + 4 + 3 + 2 = 14
5 - 4 - 3 - 2 = -4
4 + 3 + 2 + 1 = 10
4 - 3 - 2 - 1 = -2
+最大值 14/14=1.000000
-最大值 4/19=0.210526
==================================
7 + -6 + 5 + 4 + 3 = 13
7 - -6 - 5 - 4 - 3 = 1
-6 + 5 + 4 + 3 + 2 = 8
-6 - 5 - 4 - 3 - 2 = -20
5 + 4 + 3 + 2 + 1 = 15
5 - 4 - 3 - 2 - 1 = -5
+最大值 15/15=1.000000
-最大值 1/16=0.062500
==================================
7 + -6 + 5 + 4 + 3 + 2 = 15
7 - -6 - 5 - 4 - 3 - 2 = -1
-6 + 5 + 4 + 3 + 2 + 1 = 9
-6 - 5 - 4 - 3 - 2 - 1 = -21
+最大值 15/30=0.500000
-最大值 -1/14=-0.071429
==================================
7 + -6 + 5 + 4 + 3 + 2 + 1 = 16
7 - -6 - 5 - 4 - 3 - 2 - 1 = -2
+最大值 16/31=0.516129
-最大值 -2/13=-0.153846
==================================
Press any key to continue

[此贴子已经被作者于2017-2-23 17:38编辑过]

2017-02-23 17:37
宇宙规律
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:232
专家分:128
注 册:2014-5-7
收藏
得分:0 
仔细发现:和题意略有不同,但我相信,编程可以解答这样的排列组合计算题;

(m0+m1+m2)/(n0+n1+n2) = (7+-6+5)/(22+-6+5) = 6/21 = 0.285714
最大值
(m0+m1-m2)/(n0+n1-n2) = (7+-6-5)/(22+-6-5) = -4/11 = -0.363636
最大值
(m0-m1+m2)/(n0-n1+n2) = (7--6+5)/(22--6+5) = 18/33 = 0.545455
最大值
(m0-m1-m2)/(n0-n1-n2) = (7--6-5)/(22--6-5) = 8/23 = 0.347826
最大值

===============================
(m0+m1+m2+m3)/(n0+n1+n2+n3) = (7+-6+5+4)/(22+-6+5+4) = 10/25 = 0.400000
最大值
(m0+m1+m2-m3)/(n0+n1+n2-n3) = (7+-6+5-4)/(22+-6+5-4) = 2/17 = 0.117647
最大值
(m0+m1-m2+m3)/(n0+n1-n2+n3) = (7+-6-5+4)/(22+-6-5+4) = 0/15 = 0.000000
最大值
(m0+m1-m2-m3)/(n0+n1-n2-n3) = (7+-6-5-4)/(22+-6-5-4) = -8/7 = -1.142857
最大值
(m0-m1+m2+m3)/(n0-n1+n2+n3) = (7--6+5+4)/(22--6+5+4) = 22/37 = 0.594595
(m0-m1+m2-m3)/(n0-n1+n2-n3) = (7--6+5-4)/(22--6+5-4) = 14/29 = 0.482759
(m0-m1-m2+m3)/(n0-n1-n2+n3) = (7--6-5+4)/(22--6-5+4) = 12/27 = 0.444444
(m0-m1-m2-m3)/(n0-n1-n2-n3) = (7--6-5-4)/(22--6-5-4) = 4/19 = 0.210526

如果m[64],n[170],数组数据很多的情况;
2017-02-23 18:17
快速回复:求教!如何改进程序一小点,改变计算结果一大点?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.042598 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved