智力游戏(搬山游戏)

/**************************************** 
*  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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值