| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1133 人关注过本帖
标题:求教!如何修改程序,循环计算求值?
只看楼主 加入收藏
宇宙规律
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:232
专家分:128
注 册:2014-5-7
结帖率:97.06%
收藏
已结贴  问题点数:60 回复次数:12 
求教!如何修改程序,循环计算求值?
已知程序:
#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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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+m6/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-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: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:长长久久
等 级:贵宾
威 望:52
帖 子:5023
专家分:14003
注 册:2016-10-22
收藏
得分:10 
试试用数组保存数据~

具体一点就是把要比较的数记录下来~然后找出最大值~

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


[code]/*~个性签名:bug是什么意思?bug是看上去没有可能的东西实际上是有可能做到的 就是这样~2018-08-08更~*/[/code]
2017-02-22 14:04
宇宙规律
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:232
专家分:128
注 册:2014-5-7
收藏
得分:0 
回复 2楼 九转星河
mc, nc,用数组保存数据,语法逻辑怎么样合理?
2017-02-22 14:09
九转星河
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:长长久久
等 级:贵宾
威 望:52
帖 子:5023
专家分:14003
注 册:2016-10-22
收藏
得分:10 
回复 3楼 宇宙规律
你那个最大值是相对于什么而言的?用数组保存要比较的数据再从数组里面找出最大值就可以了~

[code]/*~个性签名:bug是什么意思?bug是看上去没有可能的东西实际上是有可能做到的 就是这样~2018-08-08更~*/[/code]
2017-02-22 14:25
宇宙规律
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: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:长长久久
等 级:贵宾
威 望:52
帖 子:5023
专家分:14003
注 册:2016-10-22
收藏
得分:0 
回复 5楼 宇宙规律
这个我也要想想看才行~感觉累了~需要休息一下……~看看有没有其他人帮忙解答~

[code]/*~个性签名:bug是什么意思?bug是看上去没有可能的东西实际上是有可能做到的 就是这样~2018-08-08更~*/[/code]
2017-02-22 14:44
宇宙规律
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:232
专家分:128
注 册:2014-5-7
收藏
得分:0 
回复 6楼 九转星河
劳逸结合,保持好战略能力,前方还有很多挑战;
2017-02-22 14:52
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10607
专家分:43186
注 册:2014-5-20
收藏
得分:20 
如果是都正整数,第一组m0+m1+m2+....就是最大值,否则就要计算比对。
2017-02-22 15:36
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10607
专家分:43186
注 册:2014-5-20
收藏
得分:20 
看看过程和结果是不是这样:
图片附件: 游客没有浏览图片的权限,请 登录注册

#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");
    }
}
更正

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

2017-02-22 15:39
宇宙规律
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
快速回复:求教!如何修改程序,循环计算求值?
数据加载中...
 
   



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

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