猜数游戏
  • 板块灌水区
  • 楼主2013wu
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/10/13 13:11
  • 上次更新2024/10/13 13:14:20
查看原帖
猜数游戏
988267
2013wu楼主2024/10/13 13:11

猜数游戏

#include<bits/stdc++.h>
#define int long long
using namespace std;
int x,y;
signed main(){
	srand(time(NULL));
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	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;
}


2024/10/13 13:11
加载中...