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
#include<stdio.h>
#include<string.h>
int main()
{
int n,i=0,j;
scanf("%d",&n);
while(i<n)
{
char a[1000],b[1000];
int sum[1002]={0},la,lb,q,save;
i++;
scanf("%s%s",&a,&b);
la=save=strlen(a);
lb=strlen(b);
q=la>lb?la:lb;
for(j=1;j<=la;j++)
sum[j]=a[j-1]-"0";
if(la>=lb)
{
for(j=q;lb>0;j--,lb--)
sum[j]+=(b[lb-1]-"0");
}
else
{
for(j=q;la>0;j--,lb--,la--)
sum[j]+=(b[lb-1]-"0"+a[la-1]-"0");
for(j;lb>0;lb--,j--)
sum[j]=(b[lb-1]-"0");
}
for(j=q+1;j>0;j--)
{
if(sum[j]>=10)
{
sum[j-1]++;
sum[j]-=10;
}
}
printf("Case %d:\n%s + %s = ",i,a,b);
if(sum[0]!=0)printf("%d",sum[0]);
for(j=1;j<q+1;j++)
{
printf("%d",sum[j]);
}
if(i!=n)printf("\n\n");
else printf("\n");
}
return 0;
}
20
/*
* hdu-1002
* mike-w
* 2012-5-21
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
#include<assert.h>
#endif
#define MAX_NUM_LEN 1234
#define BASE 10000
#define WIDTH 4
#define min(a,b) ((a)>(b)?(b):(a))
int conv(char *buf, int *s)
{
int len=strlen(buf);
int weight=1,pos=MAX_NUM_LEN-1;
int i;
memset(s,0,sizeof(int)*MAX_NUM_LEN);
for(i=1;i<=len;i++)
{
s[pos]+=(buf[len-i]-"0")*weight;
weight*=10;
if(weight==BASE)
weight=1, pos--;
}
s[0]=pos;
return 0;
}
int add(int *a1, int *a2, int *s)
{
memset(s,0,sizeof(int)*MAX_NUM_LEN);
int end=min(a1[0], a2[0]);
int carry=0,i;
for(i=MAX_NUM_LEN-1;i>=end;i--)
{
s[i]=a1[i]+a2[i]+carry;
carry=s[i]/BASE;
s[i]%=BASE;
}
if(carry)
s[i]=carry,s[0]=i;
else
s[0]=i+1;
return 0;
}
int disp(int *s)
{
int i=s[0];
while(i<MAX_NUM_LEN && s[i]==0)
i++;
if(i==MAX_NUM_LEN)
putchar("0");
else
printf("%d",s[i]);
for(i++;i<MAX_NUM_LEN;i++)
printf("%0*d",WIDTH,s[i]);
return 0;
}
int read_num(int *s)
{
char buf[MAX_NUM_LEN];
memset(s,0,sizeof(int)*MAX_NUM_LEN);
scanf("%s",buf);
conv(buf,s);
return 0;
}
int main(void)
{
#ifndef ONLINE_JUDGE
assert(freopen("in","r",stdin));
#endif
int ncase,ccase;
int n1[MAX_NUM_LEN];
int n2[MAX_NUM_LEN];
int s[MAX_NUM_LEN];
scanf("%d",&ncase);
for(ccase=1;ccase<=ncase;ccase++)
{
if(ccase>1)
putchar("\n");
printf("Case %d:\n",ccase);
read_num(n1);
read_num(n2);
add(n1, n2, s);
disp(n1)
printf(" + ");
disp(n2);
printf(" = ");
disp(s);
printf("\n");
}
return 0;
}
/*
* extra words:
* 本人的存储大数的方法:
* char* -> int*
* 数字在数组中与尾部对齐——不倒序存储!
* 数组的首位标示了数字可能开始的位置,有可能比实际开始位置
* 提前。
* 输出的过程中注意过滤掉数组前面的0
*/
10
#include <stdio.h>
#include <string.h>
#define MAXLEN 1000
char a1[MAXLEN];
char a2[MAXLEN];
static int v1[MAXLEN];
static int v2[MAXLEN];
static int v3[MAXLEN];
int i,j,n,L,z;
void main(void) {
scanf("%d",&n);
for (j=0;j<n;j++) {
scanf("%s%s",a1,a2);
L=strlen(a1);
for (i=0;i<L;i++) v1[i]=a1[L-1-i]-"0";
L=strlen(a2);
for (i=0;i<L;i++) v2[i]=a2[L-1-i]-"0";
for (i=0;i<MAXLEN;i++) v3[i]=v1[i]+v2[i];
for (i=0;i<MAXLEN;i++) {
if (v3[i]>=10) {
v3[i+1]+=v3[i]/10;
v3[i]=v3[i]%10;
}
}
printf("Case %d:\n", j+1);
printf("%s + %s = ", a1, a2);
z=0;
for (i=MAXLEN-1;i>=0;i--) {
if (z==0) {
if (v3[i]!=0) {
printf("%d",v3[i]);
z=1;
}
} else {
printf("%d",v3[i]);
}
}
if (z==0) printf("0");
printf("\n");
}
}
//Sample Input
//3
//0 0
//1 2
//112233445566778899 998877665544332211
//
//Sample Output
//Case 1:
//0 + 0 = 0
//Case 2:
//1 + 2 = 3
//Case 3:
//112233445566778899 + 998877665544332211 = 1111111111111111110