Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 5059
Problem descriptionas 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 (random SIVE ).
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.
Inputmulti 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 in any characters other than '\ n',' \ R '.
-1000000000 ≤ A ≤ B ≤ 1000000000
Outputfor 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 1001a0-100 100
Sample output
YESNO
Sourcebestcoder round #12
Question:
Given a string, if the string is an integer and the integer is within the range of [a, B], yes is output and no is output from other strings.
This string is an integer condition:
1. If it is a positive integer, it only contains numbers whose leading value is not 0 (there is no zero before this number ).
2. If it is a negative integer, it only contains a '-' symbol, and no leading 0 is given.
3. It is not illegal.
PS:
Note the leading zero and invalid characters!
The Code is as follows:
#include <cstdio>#include <cstring>const int maxn = 517;char str[maxn], ans[maxn];typedef __int64 LL;int flag = 0, mark = 0;LL ATOL(char s[]){ int len = strlen(s); LL tt = 0; int i = 0; if(s[0] == '-')//负号 { mark = 1; i++; } for(; i < len; i++) { if(s[i]<'0' || s[i]>'9')//非法字符 { flag = 1; break; } if(tt == 0 && s[i]=='0' && len > 1) { //前导零 flag = 1; break; } tt = tt*10+s[i]-'0'; } return tt;}int main(){ LL a, b; LL tt; while (gets(str)) { flag = 0; mark = 0; scanf("%I64d%I64d", &a, &b); getchar(); int len = strlen(str); if (len >= 13 || len == 0) { printf("NO\n"); continue; } //tt = atol(str); tt = ATOL(str); //printf("tt:: %I64d\n",tt); if(mark && tt == 0)//-0 { printf("NO\n"); continue; } if(flag) { printf("NO\n"); continue; } if(mark)//负数 tt = -tt; if (a <= tt && tt <= b) printf("YES\n"); else printf("NO\n"); } return 0;}
HDU 5059 help him (simulation)