喜极而泣!杭电OJ——1002 A + B Problem II

A + B Problem II



Problem Description
I have a very simple problem for you. 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
 

在杭电上交了12次,终于AC,掩饰不住心中的激动!特发此文,以表纪念!

这是一道大数相加的题目,测试数据已经远远超过了基本数据的表示范围,要用数组一位位相加!

以下是AC代码:

仅供参考

//设想:建立一个数组储存加的值
//经验总结:细心,细心,再细心!不然太容易出错!!!
#include<iostream>
#include<string>
#include<cstdio>
using namespace std;

int main()
{
	string a,b;
	int num,sum[1000];//数组尽量开大点,不然很容易越界!
    cin>>num;
	for(int l=0;l<num;l++)
	{
		cin>>a>>b;
		int m=0,n=0,temp=0,i,k;
		int	q=0;//用于记录sum数组的长度!!
	    i=a.length();
	    k=b.length();
		//for(i=0;a[i]!='\0';i++);
	//for(k=0;b[k]!='\0';k++);
		i=i-1;
		k=k-1;
       // cout<<"刚开始时 i="<<i<<"  "<<"k="<<k<<endl;
		while(i>=0 && k>=0)//像是这样加,会一直加到一个数加完
		{
			m=a[i]-'0';
		//	cout<<"m="<<m<<" ";
			n=b[k]-'0';
		//	cout<<"n="<<n<<endl;
		
			sum[q++]=(temp+m+n)%10;
		//	cout<<"sum[q-1]="<<sum[q-1]<<endl;
			temp=(temp+m+n)/10;//temp取进位,并且加到下一次的加法中
			//cout<<"temp="<<temp<<endl;
		//	cout<<"q="<<q<<endl;
			i--;
			k--;
		//	cout<<"i="<<i<<" "<<"k="<<k<<endl;
		//	cout<<endl;
		}
		//还需要处理一个长度的问题,判断哪个长,哪个短!
	  if(i>k)//a数比较大,确切的说是比较长!
	  {//cout<<"I am 1"<<endl;
		  while(i>=0)
		  {
		     
			 m=a[i]-'0';
			 sum[q++]=(temp+m)%10;
		     temp=(temp+m)/10;//坑爹的坑,在这里坑了一次,我本来是temp+m+n,然后不知为何错了,很难察觉的!

			 i--;
		  }
	  }
	  if(k>i)
	  {
		 // cout<<"I am 2"<<endl;
		// cout<<"k="<<k<<" "<<"temp="<<temp<<endl;
		 while(k>=0)
		  {
		     m=b[k]-'0';//这里也被坑了一次,我把b写成了a,全是复制粘贴惹的祸
			//cout<<"m="<<m<<endl;
			 sum[q++]=(temp+m)%10;
            //cout<<"sum[q-1]="<<sum[q-1]<<endl;
		     temp=(temp+m)/10;
			 k--;
		  }
	  }
	  sum[q]=temp;
	 // for(i=50;;i--)
		 // if(sum[i]!=0)
		//	  break;
		  //for(i++;i>=0;i--)
	  cout<<"Case "<<l+1<<":"<<endl;
	 cout<<a<<" + "<<b<<" = ";
	  if(sum[q]!=0) cout<<sum[q];
		  for(--q;q>=0;q--)
          cout<<sum[q];
	 //printf("%d",sum[--q]);//第一个测试数据成功!
	// for(--q;q>=0;q--)
	// {
	//	 printf("%04d",sum[q]);
	// }
	 cout<<endl;
	 if(l<num-1) cout<<endl;

	}//for
 return 0;
}


评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值