Ladder Cup Positive Integer A+B

L1-025. Positive Integer A+B

time limit
400 ms
memory limit
65536 kB
code length limit
8000 B
Judgment procedure
Standard
author
Chen Yue

The goal of this problem is very simple, which is to find the sum of two positive integers A and B, where A and B are both in the interval [1,1000]. Slightly annoying, the input is not guaranteed to be two positive integers.

Input format:

Input gives A and B on one line separated by spaces. The problem is that A and B are not necessarily positive integers that meet the requirements. Sometimes they may be out-of-range numbers, negative numbers, real numbers with decimal points, or even a bunch of garbled characters.

Note: We consider the first space appearing in the input as the separation of A and B. The title guarantees that at least one space exists and that B is not an empty string.

Output format:

If the input is indeed two positive integers, the output is in the format "A + B = sum". If a certain input does not meet the requirements, "?" is output in the corresponding position, and obviously the sum is also "?" at this time.

Input sample 1:
123 456
Sample output 1:
123 + 456 = 579
Input sample 2:
22. 18
Sample output 2:
? + 18 = ?
Input sample 3:
-100 blabla bla...33
Sample output 3:
? + ? = ?
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main(){
    char s1[1000],s2[1000];
    scanf("%s",s1);
    scanf(" ");
    gets(s2);//There may be spaces in the second string
    int a,b,flag1=1,flag2=1,sum1=0,sum2=0;
    a=strlen(s1);b=strlen(s2);
    if(a<1||a>4)
        flag1 = 0;
    if(b<1||b>4)
        flag2=0;
    if(flag1){
        for(int i=0;s1[i]!='\0';i++){
            if(s1[i]>='0'&&s1[i]<='9'){
                sum1=sum1*10+s1[i]-'0';
            }
            else{
                flag1 = 0;
                break;
            }
        }
    }
    if(flag2){
        for(int i=0;s2[i]!='\0';i++){
            if(s2[i]>='0'&&s2[i]<='9'){
                sum2=sum2*10+s2[i]-'0';
            }
            else{
                flag2=0;
                break;
            }
        }
    }
    if(flag1&&sum1>=1&&sum1<=1000)
       cout<<s1;
    else
        cout<<"?";
    cout<<" + ";
    if(flag2&&sum2>=1&&sum2<=1000)
        cout<<s2;
    else
        cout<<"?";
    cout<<" = ";
    if(flag1&&flag2&&sum1>=1&&sum1<=1000&&sum2>=1&&sum2<=1000)
        cout<<sum1+sum2<<endl;
    else
        cout<<"?"<<endl;
return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325683669&siteId=291194637