(百例编程)88.常胜将军

题目:现有21根火柴,两人轮流取,每人每次可以取走1至4根,不可多取,也不能不取,谁取最后一楰火柴谁输。请编写一个程序进行人机对弈,要求人先取,计算机后取;计算机一方为“常胜将军”。

/*
题目:现有21根火柴,两人轮流取,每人每次可以取走1至4根,不可多取,也不能不取,谁取最后一楰火柴谁输。
请编写一个程序进行人机对弈,要求人先取,计算机后取;计算机一方为“常胜将军”。
by as1138 2013-06-01
*/
#include <iostream>

using namespace std;

int main()
{

    int sum = 21;//tick num
    int input = 0;//for input
    bool bBreak = false;//if user input the wrong num is true

    //game run
    do
    {
        cout << "now the number of ticks is: " << sum << endl;
        cout << "turn to your choice(range:1~4): " << endl;
        cin >> input;
        if(input < 1 || input > 4)
        {
            cout << "your choice is out of the range(1~4)!" << endl;
            bBreak = true;
            break;
        }

        cout << "computer choices the num is: " << 5-input <<endl;
        sum -= 5;
    }while(sum > 4);

    cout << (bBreak?"because you input the wrong num," : "the num of tick is less than 4,") <<" computer is the winner!" << endl;
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值