102.搬山游戏

举报
C语言与CPP编程 发表于 2022/05/03 23:01:14 2022/05/03
【摘要】 #include<stdio.h>void main(){ int n,k,x,y,cc,pc,g; clrscr(); puts("*******************************************************"); puts("* This is a ...

  
  1. #include<stdio.h>
  2. void main()
  3. {
  4. int n,k,x,y,cc,pc,g;
  5. clrscr();
  6. puts("*******************************************************");
  7. puts("* This is a Mountain Moveing Game. *");
  8. puts("* There are n mountains, two persons move them in *");
  9. puts("* turn. Each one each time moves 1 to k mountains, the*");
  10. puts("* one who takes the last stick will lose the game. *");
  11. puts("*******************************************************");
  12. printf(" >> --------------- Game Begin ---------------------\n");
  13. pc=cc=0;
  14. g=1;
  15. for(;;)
  16. {
  17. printf(" >> No.%2d game \n",g++);
  18. printf(" >> ---------------------------------------\n");
  19. printf(" >> How many mountains are there? ");
  20. scanf("%d",&n);
  21. if(!n) break;
  22. printf(" >> How many mountains are allowed to each time? ");
  23. do{
  24. scanf("%d",&k);
  25. if(k>n||k<1) printf(" >> Repeat again!\n");
  26. }while(k>n||k<1);
  27. do{
  28. printf(" >> How many mountains do you wish move ? ");
  29. scanf("%d",&x);
  30. if(x<1||x>k||x>n) /*判断搬山数是否符合要求*/
  31. {
  32. printf(" >> Illegal,again please!\n");
  33. continue;
  34. }
  35. n-=x;
  36. printf(" >> There are %d mountains left now.\n",n);
  37. if(!n)
  38. {
  39. printf(" >> ---- I win. You are failure.----------\n\n");cc++;
  40. }
  41. else
  42. {
  43. y=(n-1)%(k+1); /*求出最佳搬山数*/
  44. if(!y) y=1;
  45. n-=y;
  46. printf(" >> Copmputer move %d mountains away.\n",y);
  47. if(n) printf(" >> There are %d mountains left now.\n",n);
  48. else
  49. {
  50. printf(" >> ---- I am failure. You win.-----------\n\n");
  51. pc++;
  52. }
  53. }
  54. }while(n);
  55. }
  56. printf(" >> Games in total have been played %d.\n",cc+pc);
  57. printf(" >> You score is win %d,lose %d.\n",pc,cc);
  58. printf(" >> My score is win %d,lose %d.\n",cc,pc);
  59. printf(" >> --------------- Game Over! ---------------------\n");
  60. printf("\n Press any key to quit...");
  61. getch();
  62. }

文章来源: blog.csdn.net,作者:程序员编程指南,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/weixin_41055260/article/details/124558614

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。