-
Title Description:
-
Implement an adder so that it can output the value of a+b.
-
Input:
-
The input includes two numbers A and b, where A and B are not more than 1000 bits in number.
-
Output:
-
There may be multiple sets of test data, for each set of data,
The value of the output a+b.
-
Sample input:
-
2 610000000000000000000 10000000000000000000000000000000
-
-
Sample output:
-
810000000000010000000000000000000
Code:
#include <iostream>#include<stdio.h>#include<string.h>using namespacestd;intans_a[1010],ans_b[1010]; voidInitintBuf[],intLen) { for(intI=0; i<len;i++) {Buf[i]=0; }} voidChartoint (CharBuf1[],intLen1,CharBuf2[],intlen2) { for(intI=0; i<len1;i++) {Ans_a[i]=buf1[i]-'0'; } for(intj=0; j<len2;j++) {Ans_b[j]=buf2[j]-'0'; }} intMain () {Chara[1010],b[1010]; intans[1010]; while(SCANF ("%s%s", A, b)! =EOF) { intLen_a=strlen (a); intlen_b=strlen (b); Init (ans_a,len_a+Ten); Init (Ans_b,len_b+Ten); Chartoint (A,len_a,b,len_b); intC=0, cnt=0; inti,j; BOOLflag=false; for(i=len_a-1, j=len_b-1; i>=0&&j>=0; I--, j--) {ans[cnt]= (ans_a[i]+ans_b[j]+c)%Ten; C= (ans_a[i]+ans_b[j]+c)/Ten; ++CNT; } if(i<0){ while(j>=0) {ans[cnt]= (ans_b[j]+c)%Ten; C= (ans_b[j]+c)/Ten; ++CNT; --J; } if(c!=0) {ans[cnt]=C; Flag=true; } } if(j<0){ while(i>=0) {ans[cnt]= (ans_a[i]+c)%Ten; C= (ans_a[i]+c)/Ten; ++CNT; --i; } if(c!=0) {ans[cnt]=C; Flag=true; } } if(flag==true){ for(intindex=cnt;index>=0; index--) {cout<<Ans[index]; } cout<<Endl; } if(flag==false){ for(intindex=cnt-1; index>=0; index--) {cout<<Ans[index]; } cout<<Endl; } } return 0;} /************************************************************** problem:1198 User:lcyvino language:c++ Re sult:accepted time:100 Ms memory:1528 kb****************************************************************/
Title 1198:a+b (large number added)