hdu 5059 Help him
2015-12-25 18:43
281 查看
Help him
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 480 Accepted Submission(s): 119
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$\leq a \leq b \leq 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
题解及代码:
题意非常easy,给一个字符串。和一个区间,看这个字符串是否能构成一个合法的整数。而且其值是否在这个区间内(注意-0为非法数据)。
首先直接排除-和-0的情况。然后推断前导0的情况,再推断0-9以外字符的情况,接着看其长度是否大于12,最后把它转化成整数,推断大小。
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> #include <map> using namespace std; typedef long long ll; bool judge(char s[],ll a,ll b) { ll num=0,d=1,i=0; int len=strlen(s),k=0; if(s[0]=='-') d=-1,k++; if(k==len||s[0]=='-'&&s[k]=='0') return false; for(i=k;i<len-1;i++) if(s[i]!='0') break; if(i!=k) return false; for(int i=k;i<len;i++) if(s[i]<'0'||s[i]>'9') return false; for(int i=k;i<len;i++) { num=num*10+s[i]-'0'; if(i-k>11) return false; } //printf("%I64d\n",num*d); if(num*d<a||num*d>b) return false; return true; } int main() { ll a,b; char s[110]; while(gets(s)!=NULL) { scanf("%I64d%I64d",&a,&b); if(a>b) swap(a,b); if(judge(s,a,b)) printf("YES\n"); else printf("NO\n"); getchar(); } return 0; }
相关文章推荐
- IOS UI-Button按钮
- I/O基础理论
- 计算其中小写字母的个数和学生成绩的判断题目
- ccf 2014-09-4
- Centos7 64 arm-linux-gcc 搭建小记
- 菜鸟初识AIDL进程通信
- Unable to connect to the MKS:Internal error
- Android Screen Monitor使用
- JavaWeb课程设计_宿舍管理系统02
- PAT刷题
- selenium2等待元素加载
- Kubernetes 之部署 cassandra + solr(datastax)解析
- 从一个案例看PL/SQL代码片的编译与运行
- 静态页面的抓取(学习简单爬虫)
- Swift类与结构、存储属性、计算属性、函数与方法、附属脚本等
- Git使用
- PAT刷题
- 博客园中前辈技术总结会不会过期
- 宏定义
- Oracle V$SESSION详解