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;
}
}
}
}
}
感谢观看