火柴问题

现有21根火柴,两人轮流取,每人每次可取走1- 4根,不可多取,也不能不取,谁取最后一根火柴则谁输。请编写一个程序进行人机对弈,要求人先取,计算机后取;计算机一方为“常胜将军”。要求程序运行效果如下图融智技术学院

  火柴问题_第1张图片

#include 

int main()
{
	printf("*turn.Each one each time takes 1 to 4 sticks.*\n");
	printf("*The one who takes the last sticks will lose the game*\n");
	printf("******************************************************\n");
	printf(">>---------------------begin game---------------------\n");
	int stick=21,mc=0,number;
	while(stick>0)
	{
		if(mc%2==0)
		{
			int ok=0;
			while(ok==0)
			{
				printf("How many sticks do you wish to take(1~%d)?",stick>=4?4:stick);
			    scanf("%d",&number);
			    if(number>=1 && number<=4 && number<=stick) ok=1;
			}
			stick-=number;
			printf("%d stick left in the pile\n",stick);
		
		}else if(mc%2==1)
		{
			number=(stick-1)%5;
			stick-=number;
			printf("Computer take %d stick\n",number);
			printf("%d stick left in the pile\n",stick);
		}
		mc++;
	}
	if(mc%2==1){
		printf("you have take the last stick\n");
		printf("sorry,you lose!\n");
	}else 
	{
		printf("Computer have take the last stick\n");
		printf("congratulation,you win!\n");
	}
	printf(">>---------------------game over---------------------\n");
	return 0;
}


你可能感兴趣的:(C/C++/MFC)