zoukankan      html  css  js  c++  java
  • L1-025 正整数A+B (15分)

    L1-025 正整数A+B (15分)

    题的目标很简单,就是求两个正整数 (A)(B) 的和,其中 (A)(B) 都在区间 ([1,1000])。稍微有点麻烦的是,输入并不保证是两个正整数。

    输入格式:

    输入在一行给出 (A)(B),其间以空格分开。问题是 (A)(B) 不一定是满足要求的正整数,有时候可能是超出范围的数字、负数、带小数点的实数、甚至是一堆乱码。
    注意:我们把输入中出现的第 (1) 个空格认为是 (A)(B) 的分隔。题目保证至少存在一个空格,并且 (B) 不是一个空字符串。

    输出格式:

    如果输入的确是两个正整数,则按格式 A + B = 和 输出。如果某个输入不合要求,则在相应位置输出 ?,显然此时和也是 ?

    输入样例1:

    123 456
    

    输出样例1:

    123 + 456 = 579
    

    输入样例2:

    22. 18
    

    输出样例2:

    ? + 18 = ?
    

    输入样例3:

    -100 blabla bla...33
    

    输出样例3:

    ? + ? = ?
    

    代码:

    #include<bits/stdc++.h>
    using namespace std;
    int x1,x2;
    bool flag1,flag2;
    string a,b,c;
    int main()
    {
        cin>>a>>b;
        if(cin>>c)flag2=1;
        if(a[0]=='0')flag1=1;
        if(b[0]=='0')flag2=1;
        for(int i=0;a[i]!='';i++)
        {
            if(!isdigit(a[i]))
            {
                flag1=1;
                break;
            }
            else x1=(x1<<3)+(x1<<1)+(a[i]^48);
        }
        for(int i=0;b[i]!='';i++)
        {
            if(!isdigit(b[i]))
            {
                flag2=1;
                break;
            }
            else x2=(x2<<3)+(x2<<1)+(b[i]^48);
        }
        if(x1>1000)flag1=1;
        if(x2>1000)flag2=1;
        if(flag1)cout<<"? + ";
        else cout<<a<<" + ";
        if(flag2)cout<<"? = ";
        else cout<<b<<" = ";
        if(flag1+flag2)cout<<'?'<<endl;
        else cout<<x1+x2<<endl;
        return 0;
    }
    
  • 相关阅读:
    Windows Store App 主题动画
    Windows Store App 过渡动画
    Windows Store App 控件动画
    Windows Store App 近期访问列表
    Windows Store App 文件选取器
    Windows Store App 访问应用内部文件
    Windows Store App 用户库文件分组
    Windows Store App 获取文件及文件夹列表
    Windows Store App 用户库文件夹操作
    Windows Store App 用户库文件操作
  • 原文地址:https://www.cnblogs.com/LengYun/p/13138077.html
Copyright © 2011-2022 走看看