hdu 5059
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.
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
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
题意:第一行给出一个字符串 判断这个字符串是否合法 如果合法是否在区间【a,b】之内(包含a,b) 坑多。。。
#include<stdio.h>
#include<string.h>
#include<queue>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
const int N=1e2+10;
const int INF=0x3f3f3f3f;
const int MOD=1e9+7;
typedef long long LL;
int main ()
{
char s[N];
LL a, b, c;
int len, flag, i, mark, legal;
while (gets(s) != 0)
{
scanf("%lld %lld%*c", &a, &b);
len = strlen(s);
mark = 0;///标记负号
legal = 1;///是否合法
c = 0;///保存字符串的值
flag = 0;///0表示在范围内 1表示不在
if (s[0] == '-') mark = 1;
for (i = 0; i < len; i++)
{
if ((s[i] == '-' && i == 0) || (s[i] >= '0' && s[i] <= '9')) continue;
/**<除了‘-’ 剩下的都必须是数字且不含有前导0的才是合法的串*/
legal = 0;
break;
}
i = 0;
if (mark) i++;
if (s[i] == '0') legal = 0;
if (len > 15) legal = 0;///串太长超出范围
if (legal)///只处理合法的串
{
i = 0;
if (mark) i++;///排除‘-’的影响
for ( ; i < len; i++)
c = c*10+(s[i]-'0');
if (mark) c = -c;///有‘-’ 取相反数
if (c >= a && c <= b) flag = 1;
}
if (len == 1 && s[0] == '0' && 0 >= a && 0 <= b) flag = 1;///字符串只有一个‘0’如果在区间内也合法
if (s[0] == '-' && len == 1) flag = 0;///只有‘-’ 也是不合法的
if (len == 0) flag = 0;///空串是不合法的!!!!!
if (flag == 0) printf("NO\n");
else printf("YES\n");
}
return 0;
}

浙公网安备 33010602011771号