大数求和问题

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 (32位整数).You may assume(假设) the length of each integer will not exceed(超过) 10000

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 in 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>
using namespace std;
int main()
{
	int a,b;
	cin>>a>>b;
	cout<<a+b<<endl;
	return 0;
}

输入程序结束条件,例如输入0 0结束本程序

#include <iostream>
using namespace std;
int main()
{
		int a,b;
		while(cin>>a>>b&&a||b)
		cout<<a+b<<endl;
	return 0;
}
输入n控制求和次数

#include <iostream>
using namespace std;
int main()
{
	int n;
	cin>>n;
	int i;
	for(i=1;i<=n;i++)
	{
		int a,b;
		cin>>a>>b;
		cout<<a+b<<endl;
	}
	return 0;
}
在加上相应的格式
#include <iostream>
using namespace std;
int main()
{
	int n;
	cin>>n;
	int i;
	for(i=1;i<=n;i++)
	{
		int a,b;
		cin>>a>>b;
		cout<<"Case "<<i<<":"<<endl;
		cout<<a<<" + "<<b<<" = "<<a+b<<endl;
		if(i!=n)
			cout<<endl;//注意最后一个数不用输出换行
	}
	return 0;
}

但是由于此题要考虑大数相加,即所谓的高精度运算(所谓的高精度运算,是指参与运算的数范围大大超出了标准数据类型(整型,实型)能表示的范围的运算。例如,求两个200位的数的和。这时,就要用到高精度算法了。)之加法。
 运算因子超出了整型、实型能表示的范围,肯定不能直接用一个数的形式来表示。在Pascal中,能表示多个数的数据类型有两种: 数组和字符串
数组:每个数组元素存储1位(在优化时,这里是一个重点!),有多少位就需要多少个数组元素;用数组表示数的优点:每一位都是数的形式,可以直接加减;运算时非常方便。用数组表示数的缺点:数组不能直接输入;输入时每两位数之间必须有分隔符,不符合数值的输入习惯;
字符串:String型字符串的最大长度是255,可以表示255位。Ansistring型字符串长度不受限制。用字符串表示数的优点:能直接输入输出,输入时,每两位数之间不必分隔符,符合数值的输入习惯;用字符串表示数的缺点:字符串中的每一位是一个字符,不能直接进行运算,必须先将它转化为数值再进行运算;运算时非常不方便;

综合以上所述,对上面两种数据结构取长补短:用字符串读入数据,用数组存储数据

#include <iostream>
#include <cstring>
using namespace std;
int main()
{
	int a1[2000],b1[2000],lena,lenb,i;
	char a[2000],b[2000];
	cin>>a>>b;
	lena=strlen(a);  
    	lenb=strlen(b);//测量输入字符串的长度
	for(i=0;i<lena;i++)  
            a1[i]=a[i]-48;  
    	for(i=0;i<lenb;i++)  
		    b1[i]=b[i]-48;//0的ASCII码是48,将字符串转换为数组,或者可以:b1[i]=b1[i]-'0'; 
	for(i=0;i<lena;i++)
		cout<<a1[i];
	for(i=0;i<lenb;i++)
		cout<<b1[i];
	return 0;
}



现在已经将输入的两个大数存入到了数组中,接下来就是处理数组了。
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int t=1;
    char a[1000],b[1000];
    int c[1000];
    while(n--)
    {
        cin>>a>>b;
        cout<<"Case "<<t<<":"<<endl;
        t++;
        cout<<a<<" + "<<b<<" = ";
        int lena=strlen(a);
        int lenb=strlen(b);
        int lenmax,i;
        if(lena>lenb)
        {
            lenmax=lena;
            for( i=lenmax-1;i>=0;i--)
            {
                b[i]=b[i-(lenmax-lenb)];
            }
            for( i=0;i<lena-lenb;i++)
            {
                b[i]='0';
            }
        }
        else if(lenb>lena)
        {
            lenmax=lenb;
            for( i=lenmax-1;i>=0;i--)
            {
                a[i]=a[i-(lenmax-lena)];
        }
            for( i=0;i<lenb-lena;i++)
            {
                a[i]='0';
            }
        }
        else
            lenmax=lena;
        int flag=0;
        int k=0;
        for( i=lenmax-1;i>=0;i--)
        {
            if((a[i]-'0')+(b[i]-'0')+flag<10)
            {
                c[k]=(a[i]-'0')+(b[i]-'0')+flag;
                flag=0;
            }
            else if((a[i]-'0')+(b[i]-'0')+flag>=10)
            {
                c[k]=((a[i]-'0')+(b[i]-'0')+flag)%10;
                flag=1;
            }
            k++;
        }
        if(flag==1)
        {
            c[k]=1;
            k++;
        }
        for( i=k-1;i>=0;i--)
            cout<<c[i];
        cout<<endl;
        if(n)
            cout<<endl;
    }
    return 0;
}






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值