对应洛谷题目
https://www.luogu.com.cn/problem/P1601
相应的函数:
reverse(str.begin(),str.end());翻转
str.find_first_not_of(‘0’);返回str中不为‘0’的剩余字符串,如果找不到,返回 npos;
tips:该代码只能解决A+B高精加法
#include<bits/stdc++.h>
using namespace std;
int main()
{
string a;
string b;
cin>>a>>b;
long long lenA=a.size();
long long lenB=b.size();
long long len=max(lenA,lenB)+10;
string str(len,'0');
reverse(a.begin(),a.end());
reverse(b.begin(),b.end());
for(int i=0;i<lenA;i++)
{
str[i]=a[i];
}
int temp=0;
for(int i=0;i<len;i++)
{
if(i<b.size())
temp+=str[i]-'0'+b[i]-'0';
else
temp+=str[i]-'0';
str[i]=temp%10+'0';
temp/=10;
}
reverse(str.begin(),str.end());
if(str.find_first_not_of('0')!= string::npos)
{
str=str.substr( str.find_first_not_of('0') );
cout<<str<<endl;
}
else cout<<0<<endl;
return 0;
}