甲和乙两人玩抽取火柴的游戏,共有21根火柴。每个人每次最多取4根火柴,最少取1根火柴。如果某个人取到最后一根火柴则输了。甲让乙先抽取,结果每次都是甲赢。
先来分析‘下常胜将军问题。甲要每次都赢,那么每次甲给乙只剩下1根火柴,因为此时乙至少取1根火柴,这样才能保证甲常胜。由于乙先抽取,因此只要保证甲抽取的数量和乙抽取的数量之和为5即可。
namespace section10_generalWin{ void generalWin(int last){ int computer,user; while(1){ printf("The current number of matchstick is %d.\n",last); printf("How many matchsticks do you choose?\n"); scanf("%d",&user); printf("Your choose %d matchsticks\n",user); if(user<1||user>4||user>last){ printf("You have been fault! Please input your choice again!\n"); continue; } last=last-user; if(last==0){ printf("The comupter has become a winner!\n"); break; }else{ computer=5-user; last=last-computer; printf("The computer chooses %d matchstick!\n",computer); if(last==0){ printf("The user has become a winner!\n"); break; } } } } void runGeneral(){ int last; printf("Please input the total number of matchstick!\n"); scanf("%d",&last); generalWin(last); }
原文:http://www.cnblogs.com/hoojjack/p/5208343.html