正在加载图片...
int l float total=1000. 0 for(i=0;i<5;i++) total= total/(1+0.0023*12) printf (" Must save % 2f at first. \n", total 104现有21根火柴,两人轮流取,每人每次可以取1~4根,不可多取,也不能不取, 取最后一根火柴者为输。设计程序进行人机对弈,如果人先取,计算机一定会赢,如果计 算机先取,机器等待人犯错误,否则,人取胜 解:此游戏取胜的关键是后走一方取子的数量与对方刚才取子的数量和为5。可以保证最 后一个子留给先取子的人。参考程序如下 #include <stdio. h> #include <stdlib. h maino i int i, s, select, total=21 printf( do you firstly take sticks?") select=getchar o f(select!='y'&&select!='Y') i i=random (3)+1 printf("Computer take %d stacks.", 1) printf (" %d sticks left. \n", total) while(total>0) printf( \nHow many sticks do you wish to take(1-%d)? total>44: total) scanf(%d",&i) I while(i>4li<ll i>total if ((total-i)<=0) printf( \nYou must taken the last stick. \n") prin 水 You lose!Game0ver.**Ⅶn") break printf("%d sticks left \n ", total-i if ((total-1)%5==1) i total-=i s=random(total>4?4: total)+{int i; float total=1000.0; for (i=0;i<5;i++) total=total/(1+0.0023*12); printf("Must save %.2f at first.\n",total); } 10.4 现有 21 根火柴,两人轮流取,每人每次可以取 1~4 根,不可多取,也不能不取, 取最后一根火柴者为输。设计程序进行人机对弈,如果人先取,计算机一定会赢,如果计 算机先取,机器等待人犯错误,否则,人取胜。 解:此游戏取胜的关键是后走一方取子的数量与对方刚才取子的数量和为 5。可以保证最 后一个子留给先取子的人。参考程序如下: #include <stdio.h> #include <stdlib.h> main() { int i,s,select,total=21; printf("Do you firstly take sticks?"); select=getchar(); if (select!='y'&&select!='Y') { i=random(3)+1; printf(" Computer take %d stacks. ",i); total-=i; printf(" %d sticks left.\n",total); } while (total>0) { do { printf("\nHow many sticks do you wish to take (1-%d)?", total>4?4:total); scanf("%d",&i); } while(i>4||i<1||i>total); if ((total-i)<=0) { printf("\nYou must taken the last stick.\n"); printf(" *** You Lose! Game Over. ***\n"); break; } printf(" %d sticks left \n ",total-i); if ((total-i)%5==1) { total-=i; s=random(total>4?4:total)+1;
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有