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:
? + ? = ?


坑点:题目中说:其中A和B都在区间[1,1000]。以为是数据的保证,而不是需要自己判断。又是谜语人的一天呢

#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int N=100005;
int j,o;
int toi(string s)
{
	int n=s.size();
	int res=0;
	for(int i=0;i<n;++i)
	{
		res*=10;
		res+=s[i]-'0';
	}
	return res;
}
bool ck(string s)
{
	for(int i=0;i<s.size();++i)
	{
		if(!isdigit(s[i]))return false;
	}
	if(toi(s)>1000||toi(s)<1)return false;
	return true;
}
int main()
{
	ios::sync_with_stdio(false);
	string a,b,p;
	getline(cin,p);
	int t=p.find(" ");
	a=p.substr(0,t);
	b=p.substr(t+1);
	
	
	if(ck(a)&&ck(b)){
		int x=toi(a);
		int y=toi(b);
		cout<<x<<" + "<<y<<" = "<<x+y;
	}
	else {
		if(ck(a))cout<<toi(a);
		else cout<<"?";
		cout<<" + ";
		if(ck(b))cout<<toi(b);
		else cout<<"?";
		cout<<" = ?";
	}
	return 0;
} 
posted @ 2022-11-17 23:02  林动  阅读(14)  评论(0编辑  收藏  举报