现在有21根火柴,2人轮流取,每人每次可以取走1~4根,不可多取,也不能不取,谁取最后一根火柴谁输。编写一个程序进行人机对弈,要求人先取,计算机后取;计算机一方为 常胜将军。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28int main(int argc, const char *argv[]) { int computer, people, spare = 21; printf("-------------------------------n"); printf("---------------begin----------------n"); printf("-------------------------------n"); while (1) { printf("---------------- 目前还有火柴%d根--------------n", spare); printf("People:"); scanf("%d", &people); if (people < 1 || people > 4 || people > spare) { printf("输入违规,取得数目有问题!nn"); continue; } spare -= people; if (spare == 0) { printf("computer win !Game Over!"); break; } computer = 5 - people; spare -= computer; printf("Computer: %d n", computer); if (spare == 0) { printf("nPeople win!Game Over!n"); break; } } return 0; }
最后
以上就是默默纸鹤最近收集整理的关于C语言-常胜将军的全部内容,更多相关C语言-常胜将军内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复