hdu 5059(模拟)

Help him

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2500    Accepted Submission(s): 518

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

题意:判断一个字符串是否符合要求:

假设为正数,不能有前导0

假设为负数,最前面有 - 号,整数部分不能有前导0

这个串必须在 [a,b]之间

这个题坑的地方:判断 0 ,一定开longlong,我就被long long 坑死了。然后还有一点就是gets()读入。

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

char str[105];
int main()
{
    while(gets(str)){
        long long a,b;
        scanf("%lld%lld",&a,&b);
        getchar();
        if(strcmp(str,"0")==0){
            if(a<=0&&b>=0) printf("YES\n");
            else printf("NO\n");
            continue;
        }
        int len = strlen(str);
        if(len>11){
            printf("NO\n");
            continue;
        }
        int s = 0;
        bool flag = false,is_nag = false;
        if(str[0]==‘-‘) {
            s++;
            is_nag = true;
        }
        long long sum = 0;
        if(str[s]==‘0‘||!isdigit(str[s])) flag = true;
        for(int i=s;i<len&&!flag;i++){
            if(isdigit(str[i])){
                sum = sum*10 + str[i]-‘0‘;
            }else{
                flag = true;
            }
        }
        if(flag){
            printf("NO\n");
        }else{
            if(is_nag) sum = -sum;
            if(sum>=a&&sum<=b){
                printf("YES\n");
            }else{
                printf("NO\n");
            }
        }
    }
    return 0;
}
时间: 2024-11-07 17:01:52

hdu 5059(模拟)的相关文章

HDU 5059 Help him(细节)

HDU 5059 Help him 题目链接 直接用字符串去比较即可,先判断原数字正确不正确,然后写一个判断函数,注意细节,然后注意判掉空串情况 代码: #include <cstdio> #include <cstring> const int N = 105; char n[N], a[N], b[N]; bool judge(char *str) { int len = strlen(str); if (len == 0) return false; int s = 0; i

HDU 4930 模拟

Fighting the Landlords Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 266    Accepted Submission(s): 87 Problem Description Fighting the Landlords is a card game which has been a heat for yea

hdu 5059

/*  * @brief hdu 5059  * @file 5059.c  * @author xiyan  * @CreatedTime 2014/11/12  * @LastChanged 2014/11/12  * @note  *      type: string convert  *  *  */ #include <stdio.h> #include <string.h> #define MAXN 110 char s[MAXN], t[MAXN]; int a, 

HDU 5059 Help him(简单模拟题)

http://acm.hdu.edu.cn/showproblem.php?pid=5059 题目大意: 给定一个字符串,如果这个字符串是一个整数,并且这个整数在[a,b]的范围之内(包括a,b),那就输出YES,其它的都是NO. 这个字符串是整数的条件: 1.如果它是正整数,它只包含前导不是0的数(这个数前面没有零). 2.如果它是负整数,只包含一个'-'符号,任然没有前导0. 3.除此之外都不是非法的 解题思路: http://bestcoder.hdu.edu.cn/ 这里有 要注意: 0

HDU 5059 Help him (模拟)

题目链接: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

BestCoder12 1002.Help him(hdu 5059) 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5059 题目意思:就是输入一行不多于 100 的字符串(除了'\n' 和 '\r' 的任意字符),问是否是合法的整数,如果是的话问是否在[a, b] 范围内,是则输出 YES,否则输出 NO 合法的整数:(1)非负整数:只有数字,没有前导0 (2)负数:负号后面只能跟着数字,负号前没有任何字符 首先这条题感觉不是出得太好,不过都是硬着头皮学人家做啦.反正一些很变态的数据可能还是过不了,但却AC的. 模

hdu 1022 模拟栈

其实就是模拟一下栈啦. 1 #include <iostream> 2 using namespace std; 3 4 const int N = 10; 5 char o1[N]; 6 char o2[N]; 7 char s[N]; 8 int ans[N * 2]; 9 10 int main () 11 { 12 int n; 13 while ( cin >> n ) 14 { 15 cin >> o1 >> o2; 16 int top = 0

hdu 4054 模拟 练习十六进制输出

http://acm.hdu.edu.cn/showproblem.php?pid=4054 貌似一般区域赛都会有一道水题 这道题PE了一次  因为输出每个数其实是两个位 如果用空格补齐的话  应该用两个空格 我用了一个空格,,, 学到: 1.%x  十六进制输出  可以输出整型,字符型等等 2.%02x  保证两位 而且会输出先导0的两位 //#pragma comment(linker, "/STACK:102400000,102400000") #include <cstd

hdu 5246(模拟)

题解:直接模拟 #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; const int N = 10005; long long n, m, k; long long s[N]; int main() { int t, cas = 1; scanf("%d", &t); while (t--) { scanf("%lld%l