首页 > 其他 > 详细

HDOJ - 1002 - A + B Problem II

时间:2014-03-03 16:41:45      阅读:360      评论:0      收藏:0      [点我收藏+]

题意:大数相加,但是AC率好低,注意进位和输出细节。

参考测试数据:

6
1 2
1 0
9999 1(找出错误的)
1 999999
5555 4445
112233445566778899 998877665544332211

代码:

#include <iostream>    
#include <iomanip>    
#include <string>    
#include <cstring>    
#include <cstdio>    
#include <queue>    
#include <stack>    
#include <algorithm>    
#include <cmath>    
#include <ctime>

using namespace std;  

const int maxn = 1000+10;
int a[maxn], b[maxn], s[maxn];
char s1[maxn], s2[maxn];

void Trans()
{
	int i = 0, j = 0;
	for(i = strlen(s1)-1, j = 0; i >=0; i--)
		a[j++] = s1[i] - ‘0‘;
	for(i = strlen(s2)-1, j = 0; i >=0; i--)
		b[j++] = s2[i] - ‘0‘;
}

void Sum()
{
	int i = 0;
	for (i = 0; i < strlen(s1) || i < strlen(s2); i++)
	{
		int temp = a[i] + b[i] + s[i];
		s[i] = temp % 10;
		s[i+1] = temp / 10;
	}
}

int main()
{
#ifdef Local      
	freopen("a.in", "r", stdin);  
#endif
	int t = 0, i = 0;
	cin >> t;
	getchar();
	for (int kase = 1; kase <= t; kase++)
	{
		memset(a, 0, sizeof(a));
		memset(b, 0, sizeof(b));
		memset(s, 0, sizeof(s));

		cout << "Case " << kase << ":" << endl;
		cin >> s1 >> s2;
		Trans();
		Sum();

		cout << s1;
		cout << " + ";

		cout << s2;
		cout << " = ";

		for (i = maxn-1; s[i] == 0 && i >= 0; i--);
		for (; i >=0; i--)
			cout << s[i] ;
		cout << endl;
		if (kase != t)
			cout << endl;
	}
}

HDOJ - 1002 - A + B Problem II,布布扣,bubuko.com

HDOJ - 1002 - A + B Problem II

原文:http://blog.csdn.net/u013545222/article/details/20307857

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!