问题及代码:
运行结果:
函数库的函数调用。
Problem J
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 1 Accepted Submission(s) : 1
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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$\leq a \leq b \leq 1000000000$
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
/*
*Copyright (c)2015,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:HDU.cpp
*作 者:单昕昕
*完成日期:2015年3月3日
*版 本 号:v1.0
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
int main()
{
char str[200],cp[200];
int a,b;
while(gets(str))
{
cin>>a>>b;
getchar();
int x=atoi(str);//把字符串转换成长整型数的一个函数,头文件是#include <cstdlib>
sprintf(cp,"%d",x);//字符串格式化命令,主要功能是把格式化的数据写入某个字符串中,头文件是#include <cstdlib>
if(strcmp(str,cp)==0 && x>=a && x<=b)//比较函数,头文件是#include <cstring>
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
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;
}