/****************************************
* File Name : game.c
* Creat Data : 2015.1.30
* Author : ZY
*****************************************/
/*智力游戏*/
/*搬山游戏*/
/*设有n座山,计算几何人为比赛的双方,轮流搬山。规定每次搬山
的数目不能超过k座,谁搬最后一座山谁输。
1.当:剩余山数目 -1<= 可移动的最大数k时,计算机要移(剩余山
数目-1),以便最后一座山留给人
2.对于任意正整数x,y,一定有:
0 <= x%(y+1)<= y在有n座山的情况下,计算机为了将最后一座山留
给人,而且要控制每次搬山数目不超过最大数k,它应搬山的数目要
满足:
(n-1)%(k+1)
如果算出结果是0,则规定只搬一座山,以防止冒进后发生问题。*/
#include <stdio.h>
int main()
{
int n,k,x,y,cc = 0,pc = 0,g = 1;
printf("Move Mountain Game\n");
printf("Game Begin\n");
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 to move away?");
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("Computer 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("Your score is win %d,lose %d.\n",pc,cc);
printf("My score is win %d,lose %d.\n",cc,pc);
return 0;
}
智力游戏(搬山游戏)
最新推荐文章于 2022-09-30 22:59:27 发布