C++写0-100猜数字小游戏

C++写0-100猜数字小游戏

比较简单,直接放出完整代码了

#include<iostream>
#include<time.h>
using namespace std;
int main()
{
 cout<<"参赛选手数目:";
 int k;
 cin>>k;
 for(int i=1;i<=k;i++)   //k个选手进行k个回合
 {
 srand((int)time(0));
 int key=rand()%100;  //生成随机数
 int count=0;
 int top=100;           //随机数所在范围上端
 int bottom=0;          //随机数所在范围下端
 int value=0;           //选手猜的数  自己输入
 int flag=1;
 cout<<"有请"<<i<<"号选手开始蒙数字!"<<endl;
 flag=1;
 while(flag)
 {
  cout<<"请在"<<bottom<<"-"<<top<<"之间猜一个数!";  //提示随机数所在范围
  cout<<"我猜数为:";
  cin>>value;
  count++;
  if(value==key)
  {
   cout<<"猜中了!共猜了"<<count<<"次。"<<endl;
   flag=0;
  }
  else
  {
   if(value>key)
   {
    top=value;   //更新随机数所在范围上端
       continue;
   }
   if(value<key)
   {
    bottom=value; //更新随机数所在范围下端
       continue;
   }
  }
 }
 }
}

感谢观看

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值