zoukankan      html  css  js  c++  java
  • 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 ' ',' '.
    -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
     
    Source
     
    题意:判断一个字符串是否符合要求:
    假设为正数,不能有前导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
    ");
                else printf("NO
    ");
                continue;
            }
            int len = strlen(str);
            if(len>11){
                printf("NO
    ");
                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
    ");
            }else{
                if(is_nag) sum = -sum;
                if(sum>=a&&sum<=b){
                    printf("YES
    ");
                }else{
                    printf("NO
    ");
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    文件的编码是一个怎样的机制
    MySQL——修改root密码的4种方法(以windows为例)
    python中nltk的下载安装方式
    VNC轻松连接远程Linux桌面
    Linux 查看磁盘分区、文件系统、磁盘的使用情况相关的命令和工具介绍
    shell脚本中>/dev/null的含义
    执行shell脚本时提示bad interpreter:No such file or directory的解决办法
    TabLayout+ViewPager的简单使用
    让listView gridView全部扩展开
    购物车中的观察者模式的应用
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5652826.html
Copyright © 2011-2022 走看看