P1601 A + B Problem (High Precision), p1601problem
Background
None
Description
High-Precision addition. x is equivalent to a + B problem. [B] [color = red] Negative numbers are not considered. [/color] [/B]
Input/Output Format
Input Format:
Enter a and B in two rows <= 10 ^ 500
Output Format:
The output contains only one row, representing the value of A + B.
Input and Output sample
Input example #1:
11
Output sample #1:
2
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 using namespace std; 6 const int MAXN=100001; 7 char a1[MAXN],b1[MAXN]; 8 int a[MAXN],b[MAXN]; 9 int ans[MAXN];10 int x;11 int main()12 {13 scanf("%s %s",a1,b1);14 int la=strlen(a1);int lb=strlen(b1);15 for(int i=0;i<la;i++)16 a[i]=a1[la-i-1]-48;17 for(int i=0;i<lb;i++)18 b[i]=b1[lb-i-1]-48;19 int i=0;20 for(i=0;i<max(la,lb);i++)21 {22 ans[i]=(a[i]+b[i]+x)%10;23 x=(a[i]+b[i]+x)/10;24 }25 ans[i]=x;26 int lc=max(la,lb);27 int flag=0;28 for(int i=lc;i>=0;i--)29 {30 if(ans[i]==0&&flag==0&&i>0)31 continue;32 else flag=1;33 printf("%d",ans[i]);34 }35 36 return 0;37 }