正整数A+B
题的目标很简单,就是求两个正整数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 <iostream> #include <cstdio> #include <cmath> #include <queue> #include <stack> #include <map> #include <algorithm> #include <vector> #include <string> #include <cstring> #include <sstream> using namespace std; char a[100],b[100]; char tmp[100]; int judge(char *s) { int len=strlen(s); if(len>4) return 0; for(int i=0;i<len;i++) { if(isdigit(s[i])==0) return 0; } int tmp; sscanf(s,"%d",&tmp); if(tmp>=1 &&tmp <=1000) return tmp; else return 0; } char input[1000]; int judgeinput() { int len=strlen(input); int cnt=0; for(int i=0;i<len;i++) { if(input[i]==' ') { cnt++; } } if(cnt==1) return 1; else return 0; } int main() { gets(input); sscanf(input,"%s%s",a,b); int x,y; x=judge(a); y=judge(b); if(judgeinput()==0) { y=0; } if(x==0 && y==0) { printf("? + ? = ?\n"); } else if(x==0 && y!=0) { printf("? + %d = ?\n",y); } else if(x!=0 && y==0) { printf("%d + ? = ?\n",x); } else { printf("%d + %d = %d\n",x,y,x+y); } return 0; }