码迷,mamicode.com
首页 > 其他好文 > 详细

杭电1002

时间:2015-07-14 17:06:24      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:

#include <iostream>
#include <string>
#include <string.h>
#include <stdlib.h>
using namespace std;

int main()
{
	int a[1002];
	int b[1002];
	int n;
	char aa[1002];
	char bb[1002];
	cin>>n;
	int line = 1;
	while (line <= n)
	{	
		cin>>aa>>bb;
		memset(a, 0, sizeof(a));       //不要硬编码,采用sizeof,因为是int是4字节,之前自己给自己埋了个坑,写成1002。。。
		memset(b, 0, sizeof(b));

		int strLenA = strlen(aa);
		for (int i = 0; i < strLenA; i++)    //数组逆转存入
		{
			a[i] = aa[strLenA - 1 - i] - ‘0‘;
		}

		int strLenB = strlen(bb);
		for (int j = 0; j < strLenB; j++)
		{
			b[j] = bb[strLenB - 1 - j] - ‘0‘;
		}

		int maxLen = strLenA > strLenB ? strLenA : strLenB;

		for (int k = 0; k < maxLen; k++)   //先处理高位
		{
			a[k+1] += (a[k] + b[k]) / 10;
			a[k] = (a[k]+ b[k]) % 10;
		}

		cout<<"Case "<<line<<":"<<endl;
		cout<<aa<<" + "<<bb<<" = ";

		if (a[maxLen] > 0)                //判断最高位是否有进位
			cout<<a[maxLen];

		for (int kk = maxLen - 1; kk >= 0; kk--)
			cout<<a[kk];

		cout<<endl;

		if (line < n)                  //注意最后一个案例不要多输出空行
			cout<<endl;
		line++;
	}
	return 0;
}

  

杭电1002

标签:

原文地址:http://www.cnblogs.com/AC-LG/p/4645514.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!