Problem Description
As you know, when you want to hack someone’s program, you must submit your test data. However sometimes you will submit invalid data, so we need a data checker to check your data. Now small W has prepared a problem for BC, but he is too busy to write the data checker. Please help him to write a data check which judges whether the input is an integer ranged from a to b (inclusive).
Note: a string represents a valid integer when it follows below rules.
1. When it represents a non-negative integer, it contains only digits without leading zeros.
2. When it represents a negative integer, it contains exact one negative sign (‘-‘) followed by digits without leading zeros and there are no characters before ‘-‘.
3. Otherwise it is not a valid integer.
Input
Multi test cases (about 100), every case occupies two lines, the first line contain a string which represents the input string, then second line contains a and b separated by space. Process to the end of file. Length of string is no more than 100. The string may contain any characters other than ‘\n’,’\r’. -1000000000
≤a≤b≤1000000000
Output
For each case output “YES” (without quote) when the string is an integer ranged from a to b, otherwise output “NO” (without quote).
Sample Input
10
-100 100
1a0
-100 100
Sample Output
YES
NO
sscanf和sprintf,两个神奇的函数,先用第一个将字符串转成数字,再用第二个转回去,如果这两个字符串还是相等,那肯定是合法的整数。一开始自己想实现这个功能,结果WA到吐血
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
#include <set>
#include <math.h>
#include <algorithm>
#include <queue>
#include <iomanip>
#define INF 0x3f3f3f3f
#define MAXN 6005
#define Mod 99999999
using namespace std;
int main()
{
char s[105],s1[105];
while(gets(s))
{
long long a,b,sum=0;
sscanf(s,"%I64d",&sum);
cin>>a>>b;
if(a>b)
swap(a,b);
sprintf(s1,"%I64d",sum);
if(strcmp(s1,s)==0&&sum>=a&&sum<=b)
printf("YES\n");
else
printf("NO\n");
getchar();
}
return 0;
}