HDU 5059 Help him (模拟)
来源:互联网 发布:人工智能 英文话题 编辑:程序博客网 时间:2024/06/06 03:57
题目链接: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 1001a0-100 100
Sample Output
YESNO
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;}
1 0
- HDU 5059 Help him (模拟)
- hdu 5059 Help him(模拟)
- HDU-#5059 Help him(字符串+模拟)
- hdu 5059 Help him(模拟)
- hdu 5059 Help him
- Help him HDU 5059
- HDU 5059 help him
- HDU 5059Help him(字符串)
- HDU-Help him-字符串的模拟
- Help him (BestCoder Round #12) HDU 5059
- hdu 5059 BC#12 Help him
- HDU—— 5059 Help him
- HDU 5059 Help him(细节)
- HDU 5059\BC 12B Help him
- HDU 5059 Help him(字符串 筛选数字 模拟)(atoi sprintf 函数)
- Help him
- Help him
- 【CUGBACM15级BC第12场 B】hdu 5059 Help him
- oracle 分页语句
- JavaScript学习笔记:DOM
- 四种在MySQL中修改root密码的方法
- 自我介绍-赵雷彧
- 【职场经验】毕业后的五年拉开大家差距的原因
- HDU 5059 Help him (模拟)
- 面向对象程序中的常量
- 第7周项目2求圆柱的表面积
- popToViewController的用法已经关闭所ViewController的方法
- EBS用户会话失效时间调整方法
- 改变你一生的30种能力-学习
- 面向连接的通信和无连接的通信
- 文件结尾eof问题
- hdu_2064_汉诺塔III