用程序解决生活中的“选择困难症”
2017-05-08 22:39
218 查看
今天和大家分享一个有趣的程序,博主编写这个程序是为了用程序帮我们做出生活中的那些困难的选择。
某参观团按以下条件限制从A、B、C、D、E五个地方中选定若干参观点:
如去A则必须去B;
D、E两地只能去一地;
B、C两地只能去一地;
C、D两地都去或都不去;
若去E地,A、D也必去。
问该团最多能去几个地方?是哪几个地方?
分析:
用变量a, b, c, d, e 表示五个地点。变量的值为1,则表示去;变量的值为0,则表示不去。
根据题意可写出表达式:
a+b==2 || a==0;
d+e != 2;
b+c != 2;
c+d==0 || c+d==2;
e+a+d==3 || e==0.
程序:
结果:
这样的程序是不是既实用又有趣呢?
今天就到这里啦,大家晚安!
某参观团按以下条件限制从A、B、C、D、E五个地方中选定若干参观点:
如去A则必须去B;
D、E两地只能去一地;
B、C两地只能去一地;
C、D两地都去或都不去;
若去E地,A、D也必去。
问该团最多能去几个地方?是哪几个地方?
分析:
用变量a, b, c, d, e 表示五个地点。变量的值为1,则表示去;变量的值为0,则表示不去。
根据题意可写出表达式:
a+b==2 || a==0;
d+e != 2;
b+c != 2;
c+d==0 || c+d==2;
e+a+d==3 || e==0.
程序:
#include <stdio.h> int main() { int a, b, c, d, e, count = 0, num, max = 0, ccount = 0, i; int array[5][6] = {0}; for (a = 1; a >= 0; a--) //穷举所有情况 for (b = 1; b >= 0; b--) for (c = 1; c >= 0; c--) for (d = 1; d >= 0; d--) for (e = 1; e >= 0; e--) { if ((a+b == 2 || a == 0) && d+e != 2 && b+c != 2 && (c+d == 0 || c+d == 2) && (e+a+d == 3 || e == 0)) //符合条件的情况 { count++; //计数 num = a+b+c+d+e; //计算能去几个地方 *(*(array + count)) = num; //二维数组每一行存储一种情况 *(*(array + count)+1) = a; *(*(array + count)+2) = b; *(*(array + count)+3) = c; *(*(array + count)+4) = d; *(*(array + count)+5) = e; //每行的第一列存储该情况能去几个地方,每行二至六列存储该情况a~f对应的值 if (num > max) //比较,得出最多能去几个地方 { max = num; 4000 } } } printf("\nThey can visit %d places at most.\n\n", max); for (i = 0; i < 5; i++) { if (max == *(*(array+i))) //若此情况为 去的地方最多 的情况,打印 { printf("plan %d :\n", ++ccount); printf("They will%s go A.\n", *(*(array+i)+1) ? "" : " not"); printf("They will%s go B.\n", *(*(array+i)+2) ? "" : " not"); printf("They will%s go C.\n", *(*(array+i)+3) ? "" : " not"); printf("They will%s go D.\n", *(*(array+i)+4) ? "" : " not"); printf("They will%s go E.\n", *(*(array+i)+5) ? "" : " not"); printf("\n"); } } return 0; }
结果:
这样的程序是不是既实用又有趣呢?
今天就到这里啦,大家晚安!
相关文章推荐
- 程序与生活:写程序是为了解决问题
- 程序与生活:写程序是为了解决问题
- 解决Windows 程序界面闪烁问题的一些经验
- dephi 程序输入法根据用户的爱好自动切换的简便解决办法
- 解决JFreeChart和一些JAVA程序在linux上不能正确显示中文
- 解决Windows 程序界面闪烁问题的一些经验
- 各位C#高手,我编了个小小关于TreeView控件的程序但就是在窗口上显示不出来.帮忙解决下.
- 解决Windows 程序界面闪烁问题的一些经验(zt)
- 分析:解决m程序转化成cpp程序的编译问题
- 解决“COM+ 无法与 Microsoft 分布式事务协调程序交谈”的问题
- 如何解决烦人的VS.NET2003编译时“无法将程序集复制到文件,另一个程序正在使用,进程无法访问”的问题?
- 解决RedHat Linux 8.0下的Kylix 3在C++ IDE模式中不能编译程序的办法
- 如何解决web程序连接不上win2003数据库服务器
- 如何解决复杂条件下的程序流的控制问题?
- 解决一个以前写的VB程序的安装问题
- .NET 程序在布署到客户机时出现异常问题的解决
- 解决RedHat Linux 8.0下的Kylix 3在C++ IDE模式中不能编译程序的办法(最简单的办法)
- 初学ASP编程易犯的一个致命程序问题及解决办
- 请求帮助:如何解决烦人的VS.NET2003编译时“无法将程序集复制到文件,另一个程序正在使用,进程无法访问”的问题?
- 程序人生: 我的十年,自己的生活经历,个人自传 (整理)