HDU 5059 Help him(字符串 筛选数字 模拟)(atoi sprintf 函数)
2016-07-19 09:09
639 查看
Help him
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2674 Accepted Submission(s): 550
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≤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
Source
BestCoder Round #12
Recommend
heyang | We have carefully selected several similar problems for you: 5722 5721 5718 5717 5716
题目大意:
给定一个字符串,字符串可以包括除'\n','\r'以外的任何字符。在给定一个区间,判断该字符串是否是正确的数字类型(不包含前导0,除了负号以外不包括任何非数字元素,-0判错,不能有空格)。
解题思路:
本来想着只是一个简单模拟的过程,不过写出来之后自己能想到的测试数据都能过,交上去之后却总是WA。最后通过看一些大神的博客才知道原来还有atoll,和sprintf这种好用的函数。用gets()输入,记得将a,b后面的回车用getchar()吃掉。将接收的字符串用atoll()函数先转为数字,这里可以遇到不合法的字符就会终止,然后再用sprintf()函数将其转化为字符串,最后用strcmp()函数进行比较是否相等,且在[a,b]范围内,则输出“YES”,反之为"NO".两种写法详见code。
简单代码:
#include <iostream> #include <cstdio> #include <cstring> #include <stdlib.h> using namespace std; const int MAXN = 100+10; char str[MAXN],cp[MAXN]; int k,a,b; int main(){ //freopen("input.txt","r",stdin); while(gets(str)){ scanf("%d%d",&a,&b); getchar(); int x=atoll(str); sprintf(cp,"%d",x); if(strcmp(str,cp)==0 && x>=a && x<=b) puts("YES"); else puts("NO"); } return 0; }
网上找的直接模拟的代码:
#include <iostream> #include <cstdio> #include <cstring> using namespace std; #define ll __int64 const int MAXN = 100+10; char str[MAXN],s[MAXN]; ll a,b,ans; bool judge(char *s){ int len=strlen(s); int id=0,sy=0; ans=0; if(s[0]=='-'){ ++id; sy=1; } if(len-id>12 || len==0) return false; if(len==1 && s[0]=='-') return false; if(s[id]<'0' || s[id]>'9') return false; if(sy){ if(s[1]=='0') return false; for(int i=id;i<len;++i){ if(s[i]<'0' || s[i]>'9') return false; ans=ans*10+s[i]-'0'; } ans=-ans; if(ans>=a && ans<=b) return true; } else{ if(s[0]=='0' && len>1) return false; for(int i=id;i<len;++i){ if(s[i]<'0' || s[i]>'9') return false; ans=ans*10+s[i]-'0'; } if(ans>=a && ans<=b) return true; } return false; } int main(){ //freopen("input.txt","r",stdin); while(gets(str)){ scanf("%I64d %I64d",&a,&b); getchar(); if(judge(str)) puts("YES"); else puts("NO"); } return 0; }
相关文章推荐
- JHUD简介(附下载地址)
- HDU 1009
- HDU1052
- HDU1039
- HDU 4990 (找规律 矩阵快速幂)
- MBProgressHUD(透明指示层)详细使用
- Cocos中的HudLayer
- Plist文件、HUD、字典转模型、纯代码封装View、XIB封装View
- c++单例 通过cocos2dx重现c++单例—— c++语法复习
- LHProgressHUD(0.1.1)
- HDU_4578 Transformation
- PAT 1060. Are They Equal
- Step5.2.1 hud 1075 What Are You Talking About
- 秋实大哥搞算数(字符串模拟)
- HDU 1237 简单计算器 && 字符串模拟四则运算
- HDU 1088 Write a simple HTML Browser && 字符串模拟
- 使用数据模型抓取和解析JSON数据
- 有关NGUI HUD Text 的使用
- MBProgressHUD进度条抖动
- *** Assertion failure in -[MBProgressHUD initWithView:],