#include<bits/stdc++.h>
using namespace std;
int n,r=10000,l=0;
int main(){
srand(time(0));
int ans=rand()*1019%10001;
while(1){
int mid=l+(r-l)/2;
if(mid<ans){
cout<<mid<<" 小"<<endl;
l=mid+1;
}
else if(mid>ans){
cout<<mid<<" 大"<<endl;
r=mid;
}
else{
cout<<mid<<" 猜对了"<<endl;
break;
}
}
return 0;
}
猜数游戏智能版
手动版
#include<bits/stdc++.h>
using namespace std;
int n;
int main(){
srand(time(0));
int ans=rand()*1019%10001;
while(1){
cin>>n;
if(n<ans) cout<<"小"<<endl;
else if(n>ans) cout<<"大"<<endl;
else{
cout<<"猜对了"<<endl;
break;
}
}
return 0;
}