zoukankan      html  css  js  c++  java
  • HDU 5059 Help him(BestCoder Round #12)

    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 ' ',' '.
    -1000000000ab1000000000
     
    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
     
    题意:这道题的题意是真的很简单,就是第一行输入一个字符串,第二行输入两个整数-1000000000 ≤ ≤ ≤ 1000000000,然后他问这个字符串在不在a~b的范围内(也就是看这个字符串能否转化为数字,能的话在不在a~b的范围内,而且不能有前导0)。
     
    那么问题就来了,这个字符串说是啥字符都有啊~~~~,还真是啥都有。。。(A完这道题,真的不知道说啥了,什么叫坑,这就是坑)
    #include<stdio.h>
    #include<string.h>
    #include<queue>
    #include<math.h>
    #include<stdlib.h>
    #include<algorithm>
    using namespace std;
    
    const int N=1e2+10;
    const int INF=0x3f3f3f3f;
    const int MOD=1e9+7;
    
    typedef long long LL;
    
    int main ()
    {
        char s[N];
        LL a, b, c;
        int len, flag, i, mark, legal;
    
        while (gets(s) != 0)
        {
            scanf("%lld %lld%*c", &a, &b);
            len = strlen(s);
            mark = 0; ///标记负号
            legal = 1; ///是否合法
            c = 0; ///保存字符串的值
            flag = 0; ///0表示在范围内 1表示不在
            if (s[0] == '-') mark = 1;
            for (i = 0; i < len; i++)
            {
                if ((s[i] == '-' && i == 0) || (s[i] >= '0' && s[i] <= '9')) continue; ///除了‘-’ 剩下的都必须是数字且不含有前导0的才是合法的串
                legal = 0;
                break;
            }
    
            i = 0;
            if (mark) i++;
            if (s[i] == '0') legal = 0;
    
            if (len > 15) legal = 0; ///串太长超出范围
    
            if (legal) ///只处理合法的串
            {
                i = 0;
                if (mark) i++; ///排除‘-’的影响
    
                for ( ; i < len; i++)
                    c = c*10+(s[i]-'0');
    
                if (mark) c = -c; ///有‘-’ 取相反数
    
                if (c >= a && c <= b) flag = 1;
            }
    
            if (len == 1 && s[0] == '0' && 0 >= a && 0 <= b) flag = 1; ///字符串只有一个‘0’如果在区间内也合法
            if (s[0] == '-' && len == 1) flag = 0; ///只有‘-’ 也是不合法的(怎么想到。。。)
            if (len == 0) flag = 0; ///空串是不合法的!!!!!(空串啊。。。坑)
    
            if (flag == 0) printf("NO
    ");
            else printf("YES
    ");
        }
    
        return 0;
    }
  • 相关阅读:
    01 Windows编程——Hello World
    图像处理基础知识
    集成IDE anaconda
    Python中的正则表达式
    Introduction of Machine Learning
    Linux命令——diff、patch
    sed & awk 概述
    Linux行编辑器——ed
    Linux命令——w、who、whoami、lastlog、last
    【问题】统计系统上有多少个用户
  • 原文地址:https://www.cnblogs.com/syhandll/p/4926116.html
Copyright © 2011-2022 走看看