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
问题描述:
一个很简单的问题,给你两个整数A和B,你的工作就是计算出A+B
输入:
第一行输入一个范围在1~20的整数T,代表测试的组数。然后再输入T行,每行两个整数A和B。注意:整数非常大,你不能用int型的变量来表示,你可以假设每个数的位数不超过1000位
输出:每组测试都需要输出两行,第一行是Case:#,#代表测试的组号,第二行输出一个等式A + B = SUM,SUM是A+B的结果,注意:在这个等式中有一些空格。在每两组测试之间输出一行空行
C语言代码1
#include <stdio.h>
void main()
{
int T=0,m=1; //m为当前测试的组号
scanf("%d",&T);//T为待测试的组数
while(T--)
{
char c[1000],a[1000],b[1000];
int x=0,y=0,t=0,v=0,j,k,z=0;
scanf("%d%d",&a,&b);//输入A和B
while(a[x])x++;//计算a的位数
while(b[y])y++; //计算b的位数
j=x<y?x:y;//j取A、B两数中较小的数的位数
for(k=0;k<j;k++,z++,x--,y--)
{
t=a[x-1]-48+b[y-1]-48+v; //从两数的末位开始相加
c[z]=48+t%10;//将两数的对应位数相加后的数字赋给数组c
v=t/10; //用来检测是否需要进位
}
if(x<y){
j=y-x;//j为A、B两数的位数之差(正数)
for(k=0;k<j;k++,z++,y--) //将位数较多的数的前几位赋给数组c
{
t=b[y-1]-48+v;
c[z]=48+t%10;
v=t/10;
}
}else{
j=x-y;
for(k=0;k<j;k++,z++,x--)
{
t=a[x-1]-48+v;
c[z]=48+t%10;
v=t/10;
}
}
if(v)//如果结果的最高位数字大于十(即最高位产生进位)
{
c[z]=48+v;//将最高位赋给数组c
z++;
c[z]=0;//数组以'\0'结束
}else c[z]=0;//数组以'\0'结束
char d[1000];
x=0;
while(c[x])x++;
j=x;//将结果的位数赋给j(注意:结果不包括最后的'\0')
printf("Case %d:\n",m);
m++;
for(k=0;k<j;k++,x--)
{
d[k]=c[x-1];//将数组c反序输出到数组d中
}
d[k]=0;//加入字符串结束符'\0'
printf("%s + %s = %s\n",a,b,d);
if(T)printf("\n");
}
}
C语言代码2
#include<stdio.h>
#include<string.h>
#define N 1005
void main()
{
char A[N],B[N],S[N];
int T;
int i,j,k,l;
int flag;
while(scanf("%d",&T)!=EOF)
{
for(i=0;i<T;i++)
{
if(i)
printf("\n");
j=0,flag=0;
scanf("%s%s",&A,&B);
k=strlen(A)-1;
l=strlen(B)-1;
while((k+1)&&(l+1))
{
S[j]=(A[k]-'0')+(B[l]-'0')+flag;
if(S[j]>=10)
{
S[j]=S[j]-10;
flag=1;
}
else flag=0;
k--,l--,j++;
}
if(k+1)
while(k>=0)
{
S[j]=(A[k]-'0')+flag;
if(S[j]>=10)
{
S[j]=S[j]-10;
flag=1;
}
else flag=0;
k--;
j++;
}
else if(l+1)
while(l>=0)
{
S[j]=(B[l]-'0')+flag;
if(S[j]>=10)
{
S[j]=S[j]-10;
flag=1;
}
else flag=0;
l--;
j++;
}
if(flag)
S[j]=flag;
else j--;
printf("Case %d:\n",i+1);
printf("%s + %s = ",A,B);
while(j>-1)
printf("%d",S[j--]);
printf("\n");
}
}
}
C语言参考代码3
// Author: Tanky Woo
// HDOJ 1002
// Accepted 1002 15MS 204K 1354B C++ Tanky Woo
#include <iostream>
#include <algorithm>
using namespace std;
int nCases;
int m[1001], n[1001];
char a[1001], b[1001];
// www.wutianqi.com
int main()
{
//freopen("input.txt", "r", stdin);
scanf("%d", &nCases);
for(int i = 1; i <= nCases; ++i)
{
memset(m, 0, sizeof(m));
memset(n, 0, sizeof(n));
getchar();
scanf("%s %s", a, b);
int len1 = strlen(a);
int len2 = strlen(b);
if(len1 > len2) //是m长度大于n
{
// 把字符数组a逆序转成int数组m
int cnt = 0;
for(int j = len1-1; j >= 0; --j)
m[cnt++] = a[j] - '0';
// 把字符数组b逆序转成int数组n
cnt = 0;
for(int j = len2-1; j >= 0; --j)
n[cnt++] = b[j] - '0';
}
else
{
// 把字符数组a逆序转成int数组n
int cnt = 0;
for(int j = len1-1; j >= 0; --j)
n[cnt++] = a[j] - '0';
// 把字符数组b逆序转成int数组m
cnt = 0;
for(int j = len2-1; j >= 0; --j)
m[cnt++] = b[j] - '0';
}
int len = len1 > len2? len2: len1;
int k = 0; //指示最高位
for(int j = 0; j < len; ++j)
{
m[j] += n[j];
if(m[j] >= 10)
k = j+1;
m[j+1] += (m[j]/10);
m[j] %= 10;
}
printf("Case %d:\n", i);
printf("%s + %s = ",a, b);
len = len1>len2? len1: len2;
if(k < len-1)
k = len-1;
for(int j = k; j >= 0; --j)
printf("%d", m[j]);
i == nCases? printf("\n"): printf("\n\n");
}
return 0;
}
C++代码1:
作者:洞庭散人
出处:http://phinecos.cnblogs.com/
#include <iostream>
#include <string>
using namespace std;
void Add(string a,string b,char sum[],int& count)
{//大数加法
int len1 = a.length();//数a的长度
int len2 = b.length();//数b的长度
int i = len1-1,j = len2-1,temp = 0,carryIn = 0;//初始进位为
count = 0;
//从最后一位开始做加法
while(i>=0&&j>=0)
{
temp = a[i]-'0'+b[j]-'0'+carryIn;//计算当前位
sum[count++] = temp%10+'0';
carryIn = temp/10;//计算进位
--i;
--j;
}
//第一个数还有剩余
if(i>=0)
{
//利用进位继续做
while(i>=0)
{
temp = a[i]-'0'+carryIn;
sum[count++] = temp%10+'0';
carryIn = temp/10;
--i;
}
}
//第二个数还有剩余
if(j>=0)
{
while(j>=0)
{
temp = b[j]-'0'+carryIn;
sum[count++] = temp%10+'0';
carryIn = temp/10;
--j;
}
}
//最高位特殊考虑下
if(carryIn>0)
{
sum[count++] = '1';
}
}
void reversePrint(char arr[],int len)
{//逆向输出
for(int i=len-1;i>=0;--i)
{
cout<<arr[i];
}
cout<<endl;
}
int main()
{
string a,b;
char sum[2000];//和
memset(sum,'0',2000);
int nCount = 0;
int caseNum,curCase=0;
cin>>caseNum;
do
{
curCase++;
cin>>a>>b;
Add(a,b,sum,nCount);
cout<<"Case "<<curCase<<":"<<endl;
cout<<a<<" + "<<b<<" = ";
reversePrint(sum,nCount);
if(curCase<caseNum)
{
cout<<endl;
}
}while(curCase<caseNum);
return 0;
}
C++代码2
// Author: Tanky Woo
// HDOJ 1002
// Accepted 1002 15MS 204K 1354B C++ Tanky Woo
#include <iostream>
#include <algorithm>
using namespace std;
int nCases;
int m[1001], n[1001];
char a[1001], b[1001];
// www.wutianqi.com
int main()
{
//freopen("input.txt", "r", stdin);
scanf("%d", &nCases);
for(int i = 1; i <= nCases; ++i)
{
memset(m, 0, sizeof(m));
memset(n, 0, sizeof(n));
getchar();
scanf("%s %s", a, b);
int len1 = strlen(a);
int len2 = strlen(b);
if(len1 > len2) //是m长度大于n
{
// 把字符数组a逆序转成int数组m
int cnt = 0;
for(int j = len1-1; j >= 0; --j)
m[cnt++] = a[j] - '0';
// 把字符数组b逆序转成int数组n
cnt = 0;
for(int j = len2-1; j >= 0; --j)
n[cnt++] = b[j] - '0';
}
else
{
// 把字符数组a逆序转成int数组n
int cnt = 0;
for(int j = len1-1; j >= 0; --j)
n[cnt++] = a[j] - '0';
// 把字符数组b逆序转成int数组m
cnt = 0;
for(int j = len2-1; j >= 0; --j)
m[cnt++] = b[j] - '0';
}
int len = len1 > len2? len2: len1;
int k = 0; //指示最高位
for(int j = 0; j < len; ++j)
{
m[j] += n[j];
if(m[j] >= 10)
k = j+1;
m[j+1] += (m[j]/10);
m[j] %= 10;
}
printf("Case %d:\n", i);
printf("%s + %s = ",a, b);
len = len1>len2? len1: len2;
if(k < len-1)
k = len-1;
for(int j = k; j >= 0; --j)
printf("%d", m[j]);
i == nCases? printf("\n"): printf("\n\n");
}
return 0;
}
