• 5820. 【NOIP提高A组模拟2018.8.16】 非法输入(模拟,字符串)


    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

    思路:

    直接模拟就好

    错误有很多种,下面加以列举

    1.非法字符

    2.非法数字(01,-0,898-之类)

    3.空格偏多(少)(    2323  23)

    4.数字过大(小)

    代码:

    #include<iostream>
    #include<cstdio>
    #define rii register int i
    #define int long long
    #define maxn 2147483647
    #define minx -2147483647
    using namespace std;
    int t;
    long long check(long long num)
    {
        if(num>maxn)
        {
            return 0;
        }
        num++;
        if(num<minx)
        {
            return 0;
        }
        return 1;
    }
    void pd(int kg,int l,int r,int wa,int wz)
    {
        if(wz<2)
        {
            puts("Input Error");
            return;
        }
        if(wa!=0)
        {
            puts("Input Error");
            return;
        }
        if(kg>1)
        {
            puts("Input Error");
            return;
        }
        l+=r;
        printf("%lld
    ",l);
    }
    signed main()
    {
        freopen("aplusb.in","r",stdin);
        freopen("aplusb.out","w",stdout);
        scanf("%lld",&t);
        getchar();
        for(rii=1;i<=t;i++)
        {
            int kg=0,num[3]={0,0,0},wz=0,wa=0,cnt=0;
            char pre=0;
            while(1)
            {
                char c;
                scanf("%c",&c);
                cnt++;
                if(cnt==1&&c!='-')
                {
                    if(c<'0'||c>'9')
                    {
                        wa++;
                    }
                }
                if(c==10)
                {
                    pd(kg,num[1],num[2],wa,wz);
                    break;
                }
                if(wa!=0)
                {
                    continue;
                }
                if(check(num[1])!=1||check(num[2])!=1)
                {
    //                puts("Input Error");
                    wa++;
    //                break;
                }
                if(c==' ')
                {
                    kg++;
                    pre=c;
                    continue;
                }
                if(c=='-')
                {
                    if(pre!=' '&&pre!=0)
                    {
                        wa++;
                    }
                    pre=c;
                    continue;
                }
                if(c<'0'||c>'9')
                {
                    wa++;
                    pre=c;
                    continue;
                }
                if(c>='0'&&c<='9')
                {
    //                if(pre=='0'&&)
    //                {
    //                    wa++;
    //                }
                    if(pre>='0'&&pre<='9')
                    {
                        num[wz]*=10;
                        if(num[wz]==0&&pre=='0')
                        {
                            wa++;
                        }                
                        if(num[wz]>maxn||(num[wz]-1)<minx)
                        {
                            continue;
                        }
                        if(num[wz]>0)
                        {
                            num[wz]+=c-'0';
                        }
                        else
                        {
                            num[wz]-=c-'0';
                        }
                    }
                    else
                    {
                        wz++;
                        num[wz]=c-'0';
                        if(wz>2)
                        {
                            wa++;
                        }
                        else
                        {
                            if(pre=='-')
                            {
                                if(num[wz]==0)
                                {
                                    wa++;
                                }
                                num[wz]*=-1;
                            }
                        }
                    }
                    pre=c;
                }
            }
        }
    }
  • 相关阅读:
    hashids typescript lua 定义文件
    luarocks 私服搭建&简单使用
    dremio 21.1 UI 新变动
    localstack 试用
    iasqlengine 基础设施即数据
    apisix 提供的一些方便的openresty lua 模块
    citus ha 参考部署方案
    citus 一些不错的资料
    minio Error: Storage resources are insufficient for the read operation 问题参考解决
    citus 11.0 beta 发布
  • 原文地址:https://www.cnblogs.com/ztz11/p/9494380.html
Copyright © 2020-2023  润新知