【WZOI】(基础题库)猜数字

题目描述:

猜数字游戏啦!给你如下四种提示:

(1)这个数严格大于x吗?

(2)这个数严格小于x吗?

(3)这个数大于等于x吗?

(4)这个数小于等于x吗?

每个提示,都会给出相应的答案,yes或者no。

如果有多个数满足条件,输出最小的。如果不存在这样的数,输出“Impossible”。

输入格式:

第一行输入一个整数n。

接下来n行,每行一个字符串“sign x answer”,是四个提示的中的一个。

sign是“>”,"<","<=",">="; answer 是“Y”或者"N" ;

如(1)就会有类似这样的字符串“  > x Y” 或者“ > x N”;

输出格式:

如果最终的答案有下界的,则输出这个下界
如果存在答案,但是答案没有下界,输出-2000000000
否则输出“Impossible”

样例输入:
4
>= 1 Y
< 3 N
<= -3 N
> 55 N
2
> 100 Y
< -100 Y
样例输出:
3
Impossible
提示:

100%的数据,n的范围[1,10000],x的范围[-109,109]

时间限制: 1000ms
空间限制: 128MB

简单的模拟题,变量l表示上界,r表示下界,随后按照题意一步一步敲代码就行了。

注意,当下界小于上界(l>r)时,要输出“Impossible”

代码实现:
#include<bits/stdc++.h>
using namespace std;
int T,l=-2e9,r=2e9,num;
string op;
char c;
int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>T;
	while(T--){
		cin>>op>>num>>c;
		if(c=='Y'){
			if(op==">")l=max(l,num+1);
			else if(op=="<")r=min(r,num-1);
			else if(op==">=")l=max(l,num);
			else if(op=="<=")r=min(r,num);
		}
		else{
			if(op==">")r=min(r,num);
			else if(op=="<")l=max(l,num);
			else if(op==">=")r=min(r,num-1);
			else if(op=="<=")l=max(l,num+1);
		}
		if(l>r)return cout<<"Impossible",0;
	}
	cout<<l;
	return 0;
}

但是这题如果你不判断c=='N'的情况也是可以AC的,有点水。

#include<bits/stdc++.h>
using namespace std;
int T,l=-2e9,r=2e9,num;
string op;
char c;
int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>T;
	while(T--){
		cin>>op>>num>>c;
		if(c=='Y'){
			if(op==">")l=max(l,num+1);
			else if(op=="<")r=min(r,num-1);
			else if(op==">=")l=max(l,num);
			else if(op=="<=")r=min(r,num);
		}
		if(l>r)return cout<<"Impossible",0;
	}
	cout<<l;
	return 0;
}

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值