杭电1002 大数相加

Problem Description
Given two integers A and B, 
your job is to calculate the Sum of A + B.
 


Input
The first line of the input contains an integer T(1<=T<=20) ,which means the number of test cases. Then T lines follow,each line consists of two positive integers, A and B.Notice that the integers are very large, that means you should not process them by using 32-bit integer. 
You may assume the length of each integer will not exceed 1000.


Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.
 
Sample Input
2
1 2
112233445566778899 998877665544332211

 
Sample Output
Case 1:
1 + 2 = 3

 Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110


 

	#include <iostream>
	#include <cstring>
	using namespace std;
	
	//字符串反转——cin是从高位开始输入,反转后从左到右由低位到高位排列,便于计算
	void invert(char *s) 
	{
		int len=strlen(s);
		for(int i=0;i<=(len-1)/2;++i)//终点是≤(起点+终点)/2 
		{
			char ch=s[i];
			s[i]=s[len-1-i];//切记最大下标到len-1 
			s[len-1-i]=ch;
		}
	}

	//求和函数,因为有多个返回值,采用指针、引用传参
	void add(char *s1,char *s2,int* ans,int &pos)//pos用来记录最高位的位置,便于输出 
	{
		int x,y,m=0;//m表示进位,个位不需要进位,所以赋初值0 
		int len1=strlen(s1);
		int len2=strlen(s2);
		int i;//易错点!!!下标i要在循环外面定义,循环外还会用到! 

        /*公共长度部分的相加*/ 
		for(i=0;i<len1&&i<len2;++i)
			{
				//易错点,要把进位加上,转化为数字计算
				//s1[i]、s2[i]要分别减去'0',转换为整数 
				x=s1[i]-'0';
				y=s2[i]-'0'; 
				//ans[i]=m+(x+y)%10;失误!!!是(进位+该位置数字和)%10 
				ans[i]=(m+x+y)%10; 
				m=(m+x+y)/10; //更新进位 
			} 
			
        /*余串处理*/
		while(i<len1) 
		{
            //本位置数值之和(进位+此位置数值)
			x=s1[i]-'0';
			ans[i]=(m+x)%10;

            //迭代下标与进位值
            ++i;
			m=(m+x)/10;//同样要考虑进位,比方该位置是9,+1进位 
			}
		while(i<len2)
		{
            //此位置数值和
			y=s2[i]-'0';
			ans[i]=(m+y)%10;

            //迭代下标与进位
            ++i;
			m=(m+y)/10; 
			} 

        /*最高位进位判断,记录最高位下标-数组右界*/
		if(m) 
			{	
				ans[i]=1;//易错点!判断加数的最高位是否要进1 
				//cout<<i<<" "<<ans[i]<<endl;测试最高为是否正确进位 
				pos=i; //最高位下标  
			}
		else pos=i-1;//循环结束时下标i多加了1,最后一位没进位的话,i应该-1 
	}
	 
	int main()
	{
        char s1[1001],s2[1001];//两个字符数组接收输入 
	    int ans[1001]={0};//整数数组的元素用来记录计算结果相应位置的数字 
	
		int Case,n,pos; //注意小写case是关键字 
		cin>>Case;
			
		for(n=1;n<=Case;++n)
			{
				ans[1001]={0};//易错点!每个新样例测试开始时必须清零!
                /*对于有多个样例且需要重复使用的变量,在新样例开始前注意初始化*/
				
				cin>>s1>>s2;	
				invert(s1);//反转数组,让低位在前,高位在后,便于进位运算 
				invert(s2);
				
				add(s1,s2,ans,pos);
				 
				invert(s1);//把s1、s2反转回来 
				invert(s2);
				
				cout<<"Case " <<n<<":"<<endl;
				cout<<s1<<" + "<<s2<<" = ";//注意+、=两边空格
				
				//for(int i=0;i<=pos;++i)//易错点!!!务必反向输出!!! 
				for(int i=pos;i>-1;--i) 
					cout<<ans[i];
				
				if(n<Case)cout<<endl<<endl;//杭电要求的格式,每个样例两个间一个空行 
				else cout<<endl; 
			}
		
	return 0;
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值