c++创意编程-猜随机数

#include <bits/stdc++.h>
#define ll long long //将long long替换为自定义名称,节省打字时间 
using namespace std;
bool QJFWPD(ll num,ll a,ll b){ //定义函数,作用是判断一个数是否在另外两个数之间,如果是则返回true,否则返回false 
	if(a<b && num>=a && num<=b) return true;
	else if(b<a && num>=b && num<=a) return true;
	else return false;
}
int main(){
	srand(time(0)); //时间初始化 
	ll a,b,guess,sjs; //定义所需变量 
	for(int i=1;i==1;i=1){ //无限循环 
		cout<<"Please enter the interval range:"<<endl; //输入提示 
		cin>>a>>b; //输入范围 
		if(a>b){ //难度不能过小,如果过小则结束程序 
			if(a-b<=10) return 0;
		}else if(a<b){
			if(b-a<=10) return 0;
		}else if(a==b) return 0;
		if(a<b) sjs=rand()%b+a; //判断输入的数那个更大,并生成对应范围的两个随机数
		else  if(a>b)sjs=rand()%a+b;
		cout<<"Please enter your guess:"<<endl; //输入提示 
		cin>>guess; //输入猜的数 
		for(int j=1;j==1;j=1){ //无限循环,如果猜对了则跳出循环继续下一次,否则告诉你大了或者小了,如果输入的数不在范围内则重新输入猜的数 
			if(QJFWPD(guess,a,b)){
        		if(guess==sjs){
					cout<<"You win!"<<endl;
					break;
				}else if(guess<sjs){
					cout<<"Your guess was small."<<endl;
					cin>>guess;
				}else if(guess>sjs){
					cout<<"Your guess was big."<<endl;
					cin>>guess;
				}
			}else{
	    		cout<<"The number entered is not in the range,please re-enter it:"<<endl;
				cin>>guess;
			}
		}
	}
	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值