任务描述
本关任务:火柴游戏。
相关知识
为了完成本关任务,你需要掌握:C 语言基础。
火柴游戏
21 根火柴游戏。现有 21 根火柴,两人轮流取,每人每次可以取 1 至 4 根,不可多取(假如多取或者取走的数量不在合法的范围内,则要求重新输入),也不能不取,谁取最后一根火柴谁输。请编写一个程序进行人机对弈,要求人先取,计算机后取;请设计一种计算机取走火柴的规则,使得计算机一方为常胜将军。
#include#include int main() { int hcs = 21,czs=0;//总数,局数 int q;//取的火柴数目 printf("Game begin:n"); while(hcs > 0) { if(czs % 2 == 0) { do { if(hcs>4) printf("How many sticks do you wish to take (1~4)?"); else printf("How many sticks do you wish to take (1~%d)?",hcs); scanf("%d",&q); }while(q>hcs||q>4); hcs = hcs - q; } else { q = (hcs - 1) % 5; printf("Computer take %d sticks.n",q); hcs = hcs - q; } if(hcs==0) printf("You have taken the last sticks.n***You lose!nGame Over." ); else printf("%d sticks left in the pile.n",hcs); ++czs; } }



