正整数A+B

正整数A+B 

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

输入格式:

输入在一行给出AB,其间以空格分开。问题是AB不一定是满足要求的正整数,有时候可能是超出范围的数字、负数、带小数点的实数、甚至是一堆乱码。

注意:我们把输入中出现的第1个空格认为是AB的分隔。题目保证至少存在一个空格,并且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; } 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值