先看题目:
接着上代码~
#include<stdio.h>
int main(void){
int toothpick = 23,player_oper,flag = 0,oper;
printf("游戏开始......\n");
while(toothpick > 0){
printf("当前的牙签数为:%d 根\n",toothpick);
printf("\n请输入你要取走的牙签数量:");
scanf("%d",&player_oper);
fflush(stdin);
while(!(player_oper > 0&&player_oper < 4)){
printf("每次取走的牙签数只能为 1 、2 或 3 哦!\n");
re: printf("请重新输入你要取走的牙签数量:");
scanf("%d",&player_oper);
fflush(stdin);
}
if(player_oper > toothpick){
printf("取走的牙签数不能超过当前剩余的牙签数");
goto re;
}
printf("\n您取走的牙签数为:%d 根\n",player_oper);
toothpick -= player_oper;
printf("当前的牙签数为:%d 根\n",toothpick);
if(toothpick == 0){
flag = 1;
break;
}else{
if(toothpick > 4){
oper = 4 - player_oper;
}else if(toothpick > 1){
oper = toothpick - 1;
}else{
oper = 1;
}
toothpick -= oper;
printf("\nAlice取走的牙签数为:%d 根\n",oper);
if(toothpick == 0){
flag = 2;
}
}
}
printf("游戏结束!");
if(flag == 1){
printf("Alice取得胜利!\n");
}else{
printf("您取得胜利!\n");
}
return 0;
}