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);
    }
  • 相关阅读:
    oracle sql日期比较:
    vs 2008 过期问题
    silverlight带有复选框的列
    SQL 把一张表虚拟成两张表
    timeupdown
    ChildWindow 父窗体交互
    Debian CentOS修改时区
    如何优雅地使用命令行设置windows文件关联
    sql复制表结构,复制表内容语句
    VC6.0 中 添加/取消 块注释的Macro代码
  • 原文地址:https://www.cnblogs.com/hoojjack/p/5208343.html
Copyright © 2011-2022 走看看