题解 | #高精度整数加法#
高精度整数加法
http://www.nowcoder.com/practice/49e772ab08994a96980f9618892e55b6
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int main()
{
string a,b;
while(cin>>a>>b)
{
if(a.length()>b.length())
{
string u=a;
a=b;
b=u;
}
int v=b.length()-a.length();
for(int i=0;i<v;i++)
{
a='0'+a;
}
reverse(a.begin(),a.end());
reverse(b.begin(),b.end());
string e;
int f=0;
long long int c=0,d=0;
for(int i=0;i<min(a.length(),b.length());i++)
{
if(f==0)
{
int g;
g=a[i]-'0'+b[i]-'0';
if(g<10)
{
char h=g+'0';
e=h+e;
f=0;
}
else if(g>=10)
{
char h=g%10+'0';
e=h+e;
f=1;
}
}
else if(f==1)
{
int g;
g=a[i]-'0'+b[i]-'0'+1;
if(g<10)
{
char h=g+'0';
e=h+e;
f=0;
}
else if(g>=10)
{
char h=g%10+'0';
e=h+e;
f=1;
}
}
}
if(f==1)
{
e='1'+e;
}
cout<<e;
}
return(0);
}
#include<algorithm>
#include<string>
using namespace std;
int main()
{
string a,b;
while(cin>>a>>b)
{
if(a.length()>b.length())
{
string u=a;
a=b;
b=u;
}
int v=b.length()-a.length();
for(int i=0;i<v;i++)
{
a='0'+a;
}
reverse(a.begin(),a.end());
reverse(b.begin(),b.end());
string e;
int f=0;
long long int c=0,d=0;
for(int i=0;i<min(a.length(),b.length());i++)
{
if(f==0)
{
int g;
g=a[i]-'0'+b[i]-'0';
if(g<10)
{
char h=g+'0';
e=h+e;
f=0;
}
else if(g>=10)
{
char h=g%10+'0';
e=h+e;
f=1;
}
}
else if(f==1)
{
int g;
g=a[i]-'0'+b[i]-'0'+1;
if(g<10)
{
char h=g+'0';
e=h+e;
f=0;
}
else if(g>=10)
{
char h=g%10+'0';
e=h+e;
f=1;
}
}
}
if(f==1)
{
e='1'+e;
}
cout<<e;
}
return(0);
}