Help him
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 248 Accepted Submission(s): 58
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
Sample Output
Source
BestCoder Round #12
Recommend
heyang | We have carefully selected several similar problems for you: 5061 5060 5057 5056 5053
SB了。。
以后 注意点用atoi... 可能过long long 用 strtoll
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char S[200];
char C[200];
int len;
long long a,b;
int getans(char *A)
{
int i;
int lena=strlen(A);
if(lena==0) return 0;
if(lena!=1&&A[0]=='0') return 0;
for(i=0;i<lena;i++)
if(!('0'<=A[i]&&A[i]<='9')) return 0;
return 1;
}
int main()
{
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
int ans;
long long k;
while(gets(S)!=NULL)
{
scanf("%I64d%I64d",&a,&b);
gets(C);
len=strlen(S);
if(S[0]=='-'&&S[1]=='0') { printf("NO\n");continue;}
if(len>11||len==0) { printf("NO\n");continue;}
if(len==11&&S[0]!='-') { printf("NO\n");continue;}
if(S[0]=='-')
ans=getans(S+1);
else ans=getans(S);
if(!ans) { printf("NO\n");continue;}
else
{
k=strtoll(S,NULL,10);
if(a<=k&&k<=b) printf("YES\n");
else printf("NO\n");
}
memset(S,0,sizeof(S));
}
return 0;
}
学长的十分美好的代码
const int N = 105;
char s[N], t[N];
int a, b, c;
bool solve(){
if(sscanf(s, "%d", &c) != 1) return 0;
sprintf(t, "%d", c);
if(strcmp(s, t) != 0) return 0;
return a<=c && c<=b;
}
int main(){
//freopen("in.txt", "r", stdin);
while(gets(s)){
scanf("%d%d", &a, &b);
getchar();
bool ans = solve();
puts(ans ? "YES" : "NO");
}
return 0;
}