| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1671 人关注过本帖
标题:求教!有7个数据,如何循环加减求值?
只看楼主 加入收藏
宇宙规律
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:232
专家分:128
注 册:2014-5-7
收藏
得分:0 
回复 9楼 吹水佬
这样可以,程序呢?呵呵!
2017-02-22 10:37
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10607
专家分:43186
注 册:2014-5-20
收藏
得分:70 
回复 11楼 宇宙规律
没测算,不知结果对否。
#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");
    }
}
2017-02-22 11:13
宇宙规律
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:232
专家分:128
注 册:2014-5-7
收藏
得分:0 
回复 12楼 吹水佬

感谢指教!应该是正确的,程序很有逻辑;条件限制很多,我自己的水平是写不出来的;

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+m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.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+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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1-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-02-22 11:29
九转星河
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:长长久久
等 级:贵宾
威 望:52
帖 子:5023
专家分:14003
注 册:2016-10-22
收藏
得分:0 
看上去像加减的排列组合问题~

[code]/*~个性签名:bug是什么意思?bug是看上去没有可能的东西实际上是有可能做到的 就是这样~2018-08-08更~*/[/code]
2017-02-22 11:51
宇宙规律
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;
   
    for (i=0; i<7; i++)
    {
        for (j=1; j<=i; j++)
            a[j] = 1;
        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];
                  
                }
            }
        
            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");
    }
}


0.318182

===============================
0.464286
0.062500

===============================
0.545455
0.347826
0.285714
-0.363636

===============================
0.594595
0.482759
0.444444
0.210526
0.400000
0.117647
0.000000
-1.142857

===============================
0.625000
0.558824
0.531250
0.423077
0.500000
0.375000
0.318182
0.062500
0.464286
0.318182
0.250000
-0.071429
0.166667
-0.250000
-0.500000
-2.750000

===============================
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
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.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
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

===============================
Press any key to continue
2017-02-22 11:58
快速回复:求教!有7个数据,如何循环加减求值?
数据加载中...
 
   



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

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