这道题,有两个错误。一个是:没有测试负数的情况;另一个也是核心错误就是MAX不应该定义成 2000000001 而应该定义成 2000000000,最后取余的时候可以用(MAX + 1)。
Wa:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
#define MAX 2000000001
int nasce,num,length,Max,Min;
string ope;
char ans;
int main()
{
while(scanf("%d",&nasce) != EOF)
{
Min = -MAX;
Max = MAX;
while(nasce--)
{
cin >> ope >> num >> ans;
if(ans == 'Y')
{
if(ope == ">")
Min = max(++num,Min);
else
if(ope == "<")
Max = min(--num,Max);
else
if(ope == ">=")
Min = max(num,Min);
else
if(ope == "<=")
Max = min(num,Max);
}
else
{
if(ope == ">")
Max = min(num,Max);
else
if(ope == "<")
Min = max(num,Min);
else
if(ope == ">=")
Max = min(--num,Max);
else
if(ope == "<=")
Min = max(++num,Min);
}
}
//cout << Min << ' ' << Max << ' ' << num << endl;
if(Max >= Min)
cout << Min % MAX<< endl;
else
cout << "Impossible" << endl;
}
return 0;
}
Ac:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
#define MAX 2000000000
int nasce,num,length,Max,Min;
string ope;
char ans;
int main()
{
while(scanf("%d",&nasce) != EOF)
{
Min = -MAX;
Max = MAX;
while(nasce--)
{
cin >> ope >> num >> ans;
if(ans == 'Y')
{
if(ope == ">")
Min = max(++num,Min);
else
if(ope == "<")
Max = min(--num,Max);
else
if(ope == ">=")
Min = max(num,Min);
else
if(ope == "<=")
Max = min(num,Max);
}
else
{
if(ope == ">")
Max = min(num,Max);
else
if(ope == "<")
Min = max(num,Min);
else
if(ope == ">=")
Max = min(--num,Max);
else
if(ope == "<=")
Min = max(++num,Min);
}
}
//cout << Min << ' ' << Max << ' ' << num << endl;
if(Max >= Min)
cout << Min % (MAX + 1) << endl;
else
cout << "Impossible" << endl;
}
return 0;
}