测试点1,3
所有数字加完之后,若有进位,还要多输出一位1
测试点5
两个数字为0的情况
#include <iostream>
#include <vector>
#include <string>
#include <cstdio>
//scanf printf防止超时
#include <algorithm>
//vector的sort
#include <sstream>
//转换
using namespace std;
#include<iomanip>
//精度
#include<cmath>
//round四舍五入取整
int main()
{
string str1,pat1,pat2;
cin>>str1>>pat1>>pat2;
if(pat1.length()<pat2.length())
{
string temp=pat1;
pat1=pat2;
pat2=temp;
}
int len=pat1.length();
int len1=pat2.length();
int flag=0;
vector<int> res;
//cout<<len<<len1<<endl;
for(int i=0;i<str1.length();i++)
{
int sum=0;
int judge=str1[str1.length()-1-i]-‘0‘;
if(judge==0)
judge=10;
//cout<<judge<<endl;
if(i<len)
{
if(i<len1)
{
sum+=pat1[pat1.length()-1-i]-‘0‘;
sum+=pat2[pat2.length()-1-i]-‘0‘;
sum+=flag;
if(sum>=judge)
{
flag=1;
sum-=judge;
//cout<<"flag"<<flag<<endl;
}else
{
flag=0;
}
}else
{
sum+=pat1[pat1.length()-1-i]-‘0‘;
sum+=flag;
if(sum>=judge)
{
flag=1;
sum-=judge;
}else
{
flag=0;
}
}
}
res.push_back(sum);
}
//测试点1,3
if(flag==1)
res.push_back(1);
int index;
for(index=res.size()-1;index>=0;index--)
{
if(res[index]!=0)
break;
}
while(index>=0)
{
cout<<res[index];
index--;
}
//测试点5
stringstream ss;
int p1,p2;
ss<<pat1;
ss>>p1;
ss.clear();
ss<<pat2;
ss>>p2;
if(p2==0&&p1==0)
cout<<"0";
cout<<endl;
return 0;
}
PAT(Basic Level) Practice : 1074 宇宙无敌加法器 (20分)
原文:https://www.cnblogs.com/zchq/p/13755675.html