• [模拟]JZOJ 5820 非法输入


    Description

    在算法竞赛中,题目一般保证了输入数据的合法性。然而在工程开发中,我们往往不期望程 序得到的输入都是合法的。
    D 君正忙着向校内 OJ 添加题目,在写了第 233 个 val.cpp 之后,她的头脑中涌现出了这样 的想法。于是她决定出一道不需要写 val.cpp 的题。
    输入两个整数,你需要做的就是输出她们的和。
     

    Input

    第一行一个正整数 T。
    接下来 T 行,每行代表一组数据。
    一组合法的数据包含由一个空格隔开的两个合法的十进制整数,行首和行尾不包含任何多余 的字符。
    一个合法的十进制整数要么是 0,要么由一个可选的负号,一个 1 到 9 之间的数字,和若干 个 0 到 9 之间的数字顺序连接而成,并且这两个数字均在区间 [−2^31 , 2^31) 之内。
    一组不合法的数据是一个不匹配以上规则的字符串。

    Output

    对于每组数据,如果该数据是合法的请输出一行一个整数代表答案,否则请输出 Input Error。
     

    Sample Input

    6
    1 1
    -1 -1
    1        1
    asdf
    
    2147483648 0

    Sample Output

    2
    -2
    Input Error
    Input Error
    Input Error
    Input Error
     

    Data Constraint

    分析

    这题按照题意模拟即可,要注意的有:单个零,前导零,-0,以及行间末符号等

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    typedef long long ll;
    ll a,b;
    int t;
    char c;
    ll Read() {
        c=getchar();ll num=0,p=1;
        bool q=0;
        while (c>'9'||c<'0') {
            if (c=='-'&&p>0) p=-1;
            else return 2147483648ll;
            c=getchar();
        }
        if (c=='0'&&p==-1) return 2147483648ll;
        if (c=='0') q=1;
        while ('0'<=c&&c<='9') {
            num=num*10+c-'0';
            if (num>2147483648ll) return 2147483648ll;
            if ('0'>c||'9'<c) return 2147483648ll;
            c=getchar();
            if ('0'<=c&&c<='9'&&q) return 2147483648ll;
        }
        if (c!=' '&&c!='
    ') return 2147483648ll;
        return num*p;
    }
    
    int main() {
        freopen("aplusb.in","r",stdin);
        freopen("aplusb.out","w",stdout);
        scanf("%d
    ",&t);
        while (t--) {
            a=Read();
            if (a>2147483647ll||a<-2147483648ll||c=='
    ') {
                printf("Input Error
    ");
                while (c!='
    ') c=getchar();
                continue;
            }
            b=Read();
            if (c!='
    ') b=2147483648ll;
            if (b>2147483647ll||b<-2147483648ll) {
                printf("Input Error
    ");
                while (c!='
    ') c=getchar();
                continue;
            }
            printf("%lld
    ",a+b);
        }
        fclose(stdin);fclose(stdout);
    }
    View Code
    在日渐沉没的世界里,我发现了你。
  • 相关阅读:
    Jsoup的使用总结
    pip install r 是什么意思
    【python3基础】相对路径,‘/’,‘./’,‘../’
    nmon安装使用及结果分析(转)
    Python raise用法(超级详细,看了无师自通)(转)
    nmon和nmon_analyser的安装(转)
    扩展:对数组进行封装,实现增删改查功能
    简单实现链表LinkedList
    ArrayList与LinkedList的区别
    使用PowerDesigner对数据库建模
  • 原文地址:https://www.cnblogs.com/mastervan/p/9491321.html
Copyright © 2020-2023  润新知