| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1134 人关注过本帖
标题:求教!如何修改程序,循环计算求值?
取消只看楼主 加入收藏
宇宙规律
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:232
专家分:128
注 册:2014-5-7
结帖率:97.06%
收藏
已结贴  问题点数:60 回复次数:6 
求教!如何修改程序,循环计算求值?
已知程序:
#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/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3+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+n3+n4-n5-n6 = 0.594595
m0+m1+m2+m3-m4+m5+m6/n0+n1+n2+n3-n4+n5+n6 = 0.594595
m0+m1+m2+m3-m4+m5-m6/n0+n1+n2+n3-n4+n5-n6 = 0.571429
m0+m1+m2+m3-m4-m5+m6/n0+n1+n2+n3-n4-n5+n6 = 0.545455
m0+m1+m2+m3-m4-m5-m6/n0+n1+n2+n3-n4-n5-n6 = 0.516129
m0+m1+m2-m3+m4+m5+m6/n0+n1+n2-n3+n4+n5+n6 = 0.571429
m0+m1+m2-m3+m4+m5-m6/n0+n1+n2-n3+n4+n5-n6 = 0.545455
m0+m1+m2-m3+m4-m5+m6/n0+n1+n2-n3+n4-n5+n6 = 0.516129
m0+m1+m2-m3+m4-m5-m6/n0+n1+n2-n3+n4-n5-n6 = 0.482759
m0+m1+m2-m3-m4+m5+m6/n0+n1+n2-n3-n4+n5+n6 = 0.482759
m0+m1+m2-m3-m4+m5-m6/n0+n1+n2-n3-n4+n5-n6 = 0.444444
m0+m1+m2-m3-m4-m5+m6/n0+n1+n2-n3-n4-n5+n6 = 0.400000
m0+m1+m2-m3-m4-m5-m6/n0+n1+n2-n3-n4-n5-n6 = 0.347826
m0+m1-m2+m3+m4+m5+m6/n0+n1-n2+n3+n4+n5+n6 = 0.545455
m0+m1-m2+m3+m4+m5-m6/n0+n1-n2+n3+n4+n5-n6 = 0.516129
m0+m1-m2+m3+m4-m5+m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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编辑过]

2017-02-22 13:50
宇宙规律
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:232
专家分:128
注 册:2014-5-7
收藏
得分:0 
回复 2楼 九转星河
mc, nc,用数组保存数据,语法逻辑怎么样合理?
2017-02-22 14:09
宇宙规律
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:232
专家分:128
注 册:2014-5-7
收藏
得分:0 
例如:m[j]最大值是m[0],m[1],m[2],m[3],m[4],m[5],m[6],m[7],中的最大值;
      m[j]+m[j+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],中的最大值;

[此贴子已经被作者于2017-2-22 14:32编辑过]

2017-02-22 14:29
宇宙规律
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:232
专家分:128
注 册:2014-5-7
收藏
得分:0 
回复 6楼 九转星河
劳逸结合,保持好战略能力,前方还有很多挑战;
2017-02-22 14:52
宇宙规律
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:232
专家分:128
注 册:2014-5-7
收藏
得分:0 
以下是引用吹水佬在2017-2-22 15:36:02的发言:

如果是都正整数,第一组m0+m1+m2+....就是最大值,否则就要计算比对。


有正整数,0,负整数;

m[j]最大值是m[0],m[1],m[2],m[3],m[4],m[5],m[6],m[7],中的最大值;

m[j]+m[j+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],中的最大值;
2017-02-22 15:42
宇宙规律
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:232
专家分:128
注 册:2014-5-7
收藏
得分:0 
回复 11楼 吹水佬


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

2017-02-22 15:54
宇宙规律
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:232
专家分:128
注 册:2014-5-7
收藏
得分:0 
回复 9楼 吹水佬
您的程序和题意100%相符,编程水平快速熟练,我很是佩服!

我的题意写错了,我希望计算的是

     m[7]={7,-6,5,4,3,2,1};
   
1:  7,-6,5,4,3,2,1, m[0]是最大值;输出m[0]/n[0];

2: 1,-1,9,7,5,3,     m[2]+m[3]是最大值,输出(m[2]+m[3])/(n[2]+n[3]);

    13,-11,1,1,1,1,m[2]-m[3]是最大值,输出(m[2]-m[3])/(n[2]-n[3]);
3:
4:
5:
6
7


[此贴子已经被作者于2017-2-22 21:31编辑过]

2017-02-22 19:33
快速回复:求教!如何修改程序,循环计算求值?
数据加载中...
 
   



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

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