高精度加法:
两个整数相加:
#include
#include
using namespace std;
const int MAX=1001;
void big_add(char a[],char b[],char c[])
{
int i,e=0,d;
int len1=strlen(a);
int len2=strlen(b);
for(i=0;i
不限制正负:
#include
#include
#include
char A[10005];
char B[10005];
int fa, fb;
void swap(char *a, int i, int j)
{
char t = a[i]-'0';
a[i] = a[j]-'0';
a[j] = t;
}
void add(char *a, char *b)
{
int la = strlen(a),lb = strlen(b);
int i,j,c=0,s,l;
for(i=fa,j=la-1; i<=j; ++i,--j) swap(a,i,j);
for(i=fb,j=lb-1; i<=j; ++i,--j) swap(b,i,j);
for(i=fa; i=fa; --i) printf("%d", a[i]);
}
int cmp(char *a, char *b)
{
int i,j,la,lb;
la = strlen(a);
lb = strlen(b);
if(la-fa>lb-fb)
return 1;
else if(la-fab[i+fb];
}
}
void minus(char *a, char *b)
{
char *t;
int i,j,ft,la,lb,c,l,s;
if(!cmp(a,b))
{
t=a; a = b; b = t;
ft = fa; fa = fb; fb = ft;
}
la = strlen(a);
lb = strlen(b);
for(i=fa,j=la-1; i<=j; ++i,--j) swap(a,i,j);
for(i=fb,j=lb-1; i<=j; ++i,--j) swap(b,i,j);
c = 0;
l = -1;
for(i=0; i+fa=0 ? 0 : 1;
a[i+fa] = (10+a[i+fa]-b[i+fb]-c)%10;
l = a[i+fa] ? i+fa : l;
c = s;
}
if(l<0)
printf("0");
else
{
if(fa) printf("-");
for(i=l; i>=fa; --i) printf("%d", a[i]);
}
}
int main()
{
scanf("%s%s", A, B);
fa = ('-'==A[0]);
fb = ('-'==B[0]);
if(fa^fb)
minus(A,B);
else
add(A,B);
}
java版:
import java.util.*;
import java.io.*;
import java.lang.String;
import java.math.BigDecimal;
public class p1036
{
public static void main(String[] args)
{
String s1,s2;
Scanner cin = new Scanner(System.in);
s1 = cin.next();
s2 = cin.next();
BigDecimal b1 = new BigDecimal(s1);
BigDecimal b2 = new BigDecimal(s2);
System.out.println(b1.add(b2));
cin.close();
}
}