PAT-练习集-L1-025. 正整数A+B

传送门:https://www.patest.cn/contests/gplt/L1-025
#include <cstdio>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#define REP(i,k,n) for(int i=k;i<n;i++)
#define REPP(i,k,n) for(int i=k;i<=n;i++)
#define scan(d) scanf("%d",&d)
#define scann(n,m) scanf("%d%d",&n,&m)
#define mst(a,k)  memset(a,k,sizeof(a));
#define LL long long
#define mod 1000000007
#define ls (g<<1)
#define rs (g<<1|1)
#define PI acos(-1.0)
#define INF 0x6f6f6f6f
using namespace std;
#define LL  long long
#define N 10005
/*
题意:注意:我们把输入中出现的第1个空格认为是A和B的分隔。题目保证至少存在一个空格,并且B不是一个空字符串。
这个提示的用意是告诉我们,第一个空格之后的字符都属于B,则有:1  1 (之间两个空格)得到 1 + ? = ?
*/
string s;
string tmp1="",tmp2="";   // 两个字符串
bool check(string s)      // 判断字符串是否满足要求
{
   int len = s.length();
   int sum=0;
   if(len>4) return 0;    // 不可能超过4位数
   for(int i=0;i<len;i++){
      if(s[i]>='0' && s[i]<='9') sum=sum*10+s[i]-'0'; 
      else return 0;      // 只可能是数字
   }
   if(sum>1000) return 0; // 结果在[1,1000]
   if(sum==0) return 0;    
   return 1;
}
int get(string s)
{
   int sum = 0;
   for(int i=0;i<s.length();i++){
      sum = sum * 10 + s[i] - '0';
   }
   return sum;
}

int main()
{
   getline(cin,s);
   int len = s.length();
   int cot = 0;
   bool flag = 0;
   for(int i=0;i<len;i++){
      if(s[i]==' '&&!flag){  //第一次出现空格,分隔两个字符串
         cot++;
         flag = 1; 
         continue;
      }
      if(cot==0) tmp1+=s[i];
      else if(cot==1) tmp2+=s[i];
   }
   if(check(tmp1)) cout<<tmp1;
   else cout<<"?";
   cout<<" + ";
   if(check(tmp2)) cout<<tmp2;
   else cout<<"?";
   cout<<" = ";
   if(check(tmp1)&&check(tmp2)) cout<<get(tmp1)+get(tmp2);
   else cout<<"?";
   return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值