第三周上机实践项目——项目4-穷举法解决组合问题

来源:互联网 发布:win7电脑优化软件 编辑:程序博客网 时间:2024/05/16 07:29
<pre name="code" class="cpp">/* *Copyright (c)2016,烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:main.cpp *作    者:郭永恒 *完成日期:2016年3月12日 *版 本 号:v1.0 * *问题描述:用穷举法解决下列问题 *(1)百钱百鸡问题 *(2)换分币 *(3)年龄几何 *(4)三色球问题 *(5)委派任务 *(6)猜数字 *(7)谁是小偷 *输入描述:无 *输出描述:无 */

问题(1):

#include <iostream>using namespace std;int main(){    int x,y;    for(x = 0; x <= 20; ++x)        for(y = 0; y <= 33; ++y)            if(7*x + 4*y == 100)                cout << x << " " << y << " " << 100-x-y << endl;    return 0;}
运行结果:


问题(2):

#include <iostream>using namespace std;int main(){    int x,y,z;    for(x = 0; x <= 100; ++x)        for(y = 0; y <= 50; ++y)            for(z = 0; z <= 20; ++z)                if(x + y*2 + z*5 == 100)                    cout << x << " " << y << " " << z <<endl;    return 0;}
运行结果:


问题(3):

#include <iostream>using namespace std;int main(){    int a,n;    for(a = 1; a <= 4; ++a)        for(n = 1; n <= 6; ++n)            if(4*n+6*a == 26 && n*(n+a)*(n+2*a)*(n+3*a) == 880)                cout << n << " " << n+a << " " << n+2*a << " " << n+3*a <<endl;    return 0;}
运行结果:

问题(4):

#include <iostream>using namespace std;int main(){    int r,w,b;    int count = 0;    for(r = 0; r <= 3; ++r)        for(w = 0; w <= 3; ++w)            for(b = 0; b <= 6; ++b)                if(r+w+b == 8)                    count++;    cout <<count <<endl;    return 0;}
运行结果:

问题(5):

#include <iostream>using namespace std;int main(){    int a,b,c,d,e,f;    for(a = 0; a < 2; ++a)    {        for(b = 0; b < 2; ++b)        {            for(c = 0; c < 2; ++c)            {                for(d = 0; d < 2; ++d)                {                    for(e = 0; e < 2; ++e)                    {                        for(f = 0; f < 2; ++f)                        {                            if(a+b >= 1 && a+d != 2 && a+e+f == 2 && (b+c==0 || b+c == 2) && c+d==1 && (d+e == 0 || d == 1))                                cout << a+b+c+d+e+f << "人" <<endl;                        }                    }                }            }        }    }    return 0;}
运行结果:


问题(6):

#include <iostream>using namespace std;int main(){    int dou,yao,xue,c;    cout <<"都"<<"要"<<"学"<<"C"<<endl;    for(dou = 0; dou <= 9; ++dou)    {        for(yao = 0; yao <= 9; ++yao)        {            for(xue = 0; xue <= 9; ++xue)            {                for(c = 0; c <= 9; ++c)                {                    if((dou*1000+yao*100+xue*10+c) + (yao*100+xue*10+c) + (xue*10+c) + c == 2008)                        cout << dou <<" "<<yao<<" "<<xue<<" "<<c<<endl;                }            }        }    }    return 0;}
运行结果:


问题(7):

#include <iostream>using namespace std;int main(){    int a,b,c,d;    for(a = 0; a < 2; ++a)        for(b = 0;b < 2; ++b)            for(c = 0; c < 2; ++c)                for(d = 0; d < 2; ++d)                    if(((a==0)+(c==1)+(d==1)+(d==0) == 3) && a+b+c+d == 1)                    {                        if(a == 1)                            cout << "A";                        if(b == 1)                            cout << "B";                        if(c == 1)                            cout << "C";                        if(d == 1)                            cout << "D";                    }    return 0;}
运行结果:



知识点总结:

深入了解了穷举法。




0 0
原创粉丝点击