zoukankan      html  css  js  c++  java
  • 算法分析之常胜将军

     

    常胜将军是一个非常有意思的智力游戏趣题,常胜将军的大意如下:

    甲和乙两人玩抽取火柴的游戏,共有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);
    }
  • 相关阅读:
    NSIS 资料
    git 强制刷新,放弃更改
    Using 1.7 requires compiling with Android 4.4 (KitKat); currently using API 8
    …gen already exists but is not a source folder. Convert to a source folder or rename it [closed]
    eclipse
    Timeout in android httpclient
    git command
    L1-032. Left-pad
    L1-030. 一帮一
    L1-028. 判断素数
  • 原文地址:https://www.cnblogs.com/hoojjack/p/5208343.html
Copyright © 2011-2022 走看看