常胜将军是一个非常有意思的智力游戏趣题,常胜将军的大意如下:
甲和乙两人玩抽取火柴的游戏,共有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. ",last); printf("How many matchsticks do you choose? "); scanf("%d",&user); printf("Your choose %d matchsticks ",user); if(user<1||user>4||user>last){ printf("You have been fault! Please input your choice again! "); continue; } last=last-user; if(last==0){ printf("The comupter has become a winner! "); break; }else{ computer=5-user; last=last-computer; printf("The computer chooses %d matchstick! ",computer); if(last==0){ printf("The user has become a winner! "); break; } } } } void runGeneral(){ int last; printf("Please input the total number of matchstick! "); scanf("%d",&last); generalWin(last); }