蓝桥杯 BASIC 29 高精度加法(大数)

【思路】:大数处理都一样。

【AC代码】:代码细节能够美化一下。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iomanip>
using namespace std;

#define MAX 100+10

int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	int a[MAX], b[MAX], i = 0, alen = 0, blen = 0;
	char str[MAX];
	//initial
	memset(a, 0, sizeof(a));
	memset(b, 0, sizeof(b));
	
	//input
	cin >> str;
	alen = strlen(str);
	for (i = 0; i < alen; i++)
		a[alen-1-i] = str[i]-'0';
	cin >> str;
	blen = strlen(str);
	for (i = 0; i < blen; i++)
		b[blen-1-i] = str[i]-'0';
	
	//cal
	int temp = 0;
	for (i = 0; i < (alen>blen?alen:blen); i++)
	{
		temp = a[i]+b[i]+temp;
		a[i] = temp % 10;
		temp = temp / 10;
	}
	if (0!=temp)
		a[i++] = temp;
		
	//output
	for (i--; i >=0; i--)
		cout << a[i];
	return 0;
}



posted @ 2016-02-28 08:19  yxwkaifa  阅读(323)  评论(0编辑  收藏  举报