1. 程式人生 > >【高精度】簡單高精度加法(大數加法)

【高精度】簡單高精度加法(大數加法)

問題 B: 【高精度】簡單高精度加法

時間限制: 1 Sec  記憶體限制: 64 MB

題目描述

修羅王解決了計算機的記憶體限制問題,終於可以使用電腦進行大型的魔法運算了,他交給邪狼的第一個任務是計算兩個非負整數A、B的和,其中A和B的位數在5000位以內。

 

輸入

共兩行資料,第一行為一個非負整數A,第二行為一個非負整數B,A、B的位數均在5000以內。

 

輸出

輸出一個非負數,即兩數之和。

 

樣例輸入

複製樣例資料

1111111111
2222222222

樣例輸出

3333333333

 

/**/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <queue>

typedef long long LL;
using namespace std;

char a[50005], b[50005];
int ans[50005];

int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);

	scanf("%s %s", a, b);
	int lena = strlen(a), lenb = strlen(b);
	int cnt = 0;
	while(lena - cnt > 0 || lenb - cnt > 0){
		if(lena - cnt > 0) ans[cnt] += a[lena - cnt - 1] - '0';
		if(lenb - cnt > 0) ans[cnt] += b[lenb - cnt - 1] - '0';
		ans[cnt + 1] += ans[cnt] / 10;
		ans[cnt++] %= 10;
	}
	while(ans[cnt]) cnt++;
	for (int i = cnt - 1; i >= 0; i--){
		printf("%d", ans[i]);
	}
	printf("\n");

	return 0;
}
/**/