102.搬山游戏
【摘要】
#include<stdio.h>void main(){ int n,k,x,y,cc,pc,g; clrscr(); puts("*******************************************************"); puts("* This is a ...
-
#include<stdio.h>
-
void main()
-
{
-
int n,k,x,y,cc,pc,g;
-
clrscr();
-
puts("*******************************************************");
-
puts("* This is a Mountain Moveing Game. *");
-
puts("* There are n mountains, two persons move them in *");
-
puts("* turn. Each one each time moves 1 to k mountains, the*");
-
puts("* one who takes the last stick will lose the game. *");
-
puts("*******************************************************");
-
printf(" >> --------------- Game Begin ---------------------\n");
-
pc=cc=0;
-
g=1;
-
for(;;)
-
{
-
printf(" >> No.%2d game \n",g++);
-
printf(" >> ---------------------------------------\n");
-
printf(" >> How many mountains are there? ");
-
scanf("%d",&n);
-
if(!n) break;
-
printf(" >> How many mountains are allowed to each time? ");
-
do{
-
scanf("%d",&k);
-
if(k>n||k<1) printf(" >> Repeat again!\n");
-
}while(k>n||k<1);
-
do{
-
printf(" >> How many mountains do you wish move ? ");
-
scanf("%d",&x);
-
if(x<1||x>k||x>n) /*判断搬山数是否符合要求*/
-
{
-
printf(" >> Illegal,again please!\n");
-
continue;
-
}
-
n-=x;
-
printf(" >> There are %d mountains left now.\n",n);
-
if(!n)
-
{
-
printf(" >> ---- I win. You are failure.----------\n\n");cc++;
-
}
-
else
-
{
-
y=(n-1)%(k+1); /*求出最佳搬山数*/
-
if(!y) y=1;
-
n-=y;
-
printf(" >> Copmputer move %d mountains away.\n",y);
-
if(n) printf(" >> There are %d mountains left now.\n",n);
-
else
-
{
-
printf(" >> ---- I am failure. You win.-----------\n\n");
-
pc++;
-
}
-
}
-
}while(n);
-
-
}
-
printf(" >> Games in total have been played %d.\n",cc+pc);
-
printf(" >> You score is win %d,lose %d.\n",pc,cc);
-
printf(" >> My score is win %d,lose %d.\n",cc,pc);
-
printf(" >> --------------- Game Over! ---------------------\n");
-
printf("\n Press any key to quit...");
-
getch();
-
}
文章来源: blog.csdn.net,作者:程序员编程指南,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/weixin_41055260/article/details/124558614
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)