Contest1015 - 2017年春季C++作业一 Problem A: 猜数游戏

Description

计算机随机产生一个数A,大于等于1小于等于100,让你猜A是多少,屏幕提示为:
 I have a number between 1 and 100.
 Can you guess my number?
 Please type your guess!

接下来要用cin语句通过键盘输入猜的数B1,计算机将A与B1对照之后给出是相等、不相等、大了还是小了的信息,这些信息为:
 Good! You guessed the number.
 Too low. Try again please!
 Too high. Try again please!

根据这个信息,不断地用cin语句通过键盘输入第2次猜的数B2,第3次猜的数B3,...,直至猜中为止。计算机给出评价,猜的次数越少当然越好。评价等级的相关规则请自行拟定。请自行设计并编写三个函数,分别实现出题、竞猜、评分功能。

Input

Output

HINT

提示:本题只需上交代码,系统无法测试。

#include<ctime>
#include<cstdlib>
using namespace std;

int getrand(void)
{
  return rand()%101+1;
}
int  guess(int a)
{
    int n;
    cin>>n;
    return a-n;
}
bool judge(int a)
{
    if(a>0)
    {
        cout<<"Too low. Try again please!"<<endl;
        return true;
    }
    else if(a==0)
    {
        cout<<"Good! You guessed the number!"<<endl;
        return false;
    }
    else
    {
        cout<<"Too high. Try again please!"<<endl;
        return true;
    }
}
void Rank(int cnt)
{
    if(cnt<3)
    {
        cout<<"Goodboy!"<<endl;
    }
    else if(cnt<8)
    {
        cout<<"you can do better!"<<endl;
    }
    else
    {
        cout<<"maybe you should learn the dichotomy algorithm!"<<endl;
    }
}
int main()
{
    srand(time(NULL));
    int n,cnt=0;
    cout<<"I have a number between 1 and 100."<<endl;
    cout<<"Can you guess my number?"<<endl;
    cout<<"Please type your guess!"<<endl;
    n=getrand();
    while(judge(guess(n)))
    {
        cnt++;
    }
    Rank(cnt);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值