猜数游戏
#include<bits/stdc++.h>
#define int long long
using namespace std;
int x,y;
signed main(){
srand(time(NULL));
cout<<"猜数游戏"<<endl<<endl;
cout<<"请给定范围[x,y](x<=y)"<<endl;
cin>>x>>y;
int f=x+rand()%(y-x+1);
int n;
int ans=0;
while(cin>>n){
ans++;
if(n<x||n>y){
cout<<"超出范围,请重新输入!"<<endl;
cin>>n;
}
if(n==f||x==y){
cout<<"已猜中\n共用"<<ans<<"次"<<endl;
return 0;
}
if(n<f){
cout<<n<<"到"<<y<<endl;
x=n;
}else{
cout<<x<<"到"<<n<<endl;
y=n;
}
}
return 0;
}