1202:A + B Problem (Big integer version) 【高精度】
Time Limit:1000MS Memory Limit:65536K
Total Submit:491 Accepted:79 Page View:235
Description
Input and output are the same with problem 1001.
But A and B are big non-negative integers.
The biggest integer is less than 10^500.
Input
Input contains multiple test cases.
Each case have two big non-negative integers a,b.
Output
For each case,Output a+b.
Sample Input
1234567890987654321
9876543210123456789
123456789123456789
321654987321654987
908132111069327034363307369747425614356355845871 897674675383053803206222 208572297412176860430561 392174558003740925981195 265531007548716379717949 045703916959416008843057 167496049883408581292045 791645374701946164403139 530792062494734995105353 008614648630719815559076 346642939267370952542851 097327260060898121976009 937467598293376684547350 947367647078834228133877 919179249590039375120953 9300628363443012
653800586266491307481365622064384244384413190575 456567207535839113553710 879599163815547445261087 430974286723136050254230 838219905367559282524078 861399189856727711688179 374934080772833579539430 126162947987054873645098 400340159470592317831490 619591482513697328131486 228945410074523776903441 005708070311129960512711 459455292120992889151524 251562032482805591285422 750752571798135144747357 0262981491527798
Sample Output
11111111101111111110
445111776445111776
156193269733581834184467299181180985874076903644 735424188291889291675993 308817146122772430569164 882314884472687697623542 610375091291627566224202 790710310681614372053123 654243013065624216083147 591780832268900103804823 793113222196532731293684 362820613114441714369056 257558834934189472944629 210303533037202808248872 139692289041436957369887 519892967956163981941930 066993182138817451986831 09563609854970810
Hint
Note that there are leading zeros!
解题思路
首先要解决的就是存储1000位整数的问题。
显然,任何C/C++固有类型的变量都无法保存它。
最直观的想法是可以用一个字符串来保存它。
字符串本质上就是一个字符数组,因此为了编程更方便,我们也可以用数组unsigned an[1001]来保存一个1000位的整数
让an[0]存放个位数,an[1]存放十位数,an[2]存放百位数
那么如何实现两个大整数相加呢?
方法很简单,就是模拟小学生列竖式做加法,从个位开始逐位相加,超过或达到10则进位。
也就是说,用unsigned an1[1001]保存第一个数,用unsigned an2[1000]表示第二个数,然后逐位相加,相加的结果直接存放在an1中。
要注意处理进位。
#include<iostream>
#include<cstring>
using namespace std;
#define max 1001
int a[max+10];
int b[max+10];
char x[max+10];
char y[max+10];
int main()
{
}