大数加法

c++版:

 #include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define MAX(a,b) (a > b ? a : b)
#define MIN(a,b) (a < b ? a : b)
#define mem0(a) memset(a,0,sizeof(a)) typedef long long LL;
const double eps = 1e-;
const int MAXN = ;
const int MAXM = ; char a[MAXN], b[MAXN]; void slove()
{
int lena = strlen(a);
int lenb = strlen(b);
strrev(a); strrev(b);
int p = ;
while(a[p] || b[p])
{
if(a[p]>='') a[p] -= '';
if(b[p]>='') b[p] -= '';
a[p] += b[p];
a[p+] += a[p]/;
a[p] = a[p] % + '';
p++;
}
strrev(a);
} int main()
{
int T, Case=;
scanf("%d", &T);
while(T--)
{
mem0(a); mem0(b);
scanf("%s %s%*c", a, b);
printf("Case %d:\n", ++Case);
printf("%s + %s = ", a, b);
slove();
printf("%s\n", a);
if(T) printf("\n");
}
return ;
}

java版:

 import java.math.*;
import java.util.*;
import java.io.*; public class Main{ public static void main(String[] args) throws Exception
{ Scanner cin = new Scanner(new BufferedInputStream(System.in));
int T = cin.nextInt();
int c = 1;
while((T--) != 0)//多组输入
{
BigInteger a,b;
a = cin.nextBigInteger();
b = cin.nextBigInteger();
System.out.println("Case " + (c++) + ":");
System.out.println(a + " + " + b + " = " + a.add(b));
if(T != 0) System.out.println();
}
}
}
05-02 10:16