首页 > 代码库 > HDU 5059 Help him (模拟)
HDU 5059 Help him (模拟)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=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≤a≤b≤1000000000
Length of string is no more than 100.
The string may contain any characters other than ‘\n‘,‘\r‘.
-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
Source
BestCoder Round #12
题意:
给定一个字符串,如果这个字符串是一个整数,并这个整数在[a,b]的范围之内,输出YES,其它的输出NO。
这个字符串是整数的条件:
1、如果它是正整数,它只包含前导不是0的数(这个数前面没有零)。
2、如果它是负整数,只包含一个‘-‘符号,任然没有前导0。
3、除此之外都不是非法的
PS:
注意前导零,和非法字符!
代码如下:
#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 (模拟)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。