给出2个大整数A,B,计算A+B的结果。
JAVA版1:
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();
}
}
JAVA版2:
import java.io.PrintWriter;
import java.util.Scanner;
public class BigNumberPlus {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
String a = in.nextLine();
String b = in.nextLine();
boolean negtiveA = false;
String aPositive = a;
if (a.charAt(0) == '-') {
negtiveA = true;
aPositive = a.substring(1);
}
boolean negtiveB = false;
String bPositive = b;
if (b.charAt(0) == '-') {
negtiveB = true;
bPositive = b.substring(1);
}
String result;
if (negtiveA == false && negtiveB == false) {
result = plus(aPositive, bPositive);
} else if (negtiveA == true && negtiveB == true) {
result = plus(aPositive, bPositive);
if (!(result.length() == 1 && result.charAt(0) == '0')) {
result = "-" + result;
}
} else if (negtiveA == false && negtiveB == true) {
result = substract(aPositive, bPositive);
} else {
result = substract(bPositive, aPositive);
}
out.println(result);
out.flush();
System.exit(0);
}
private static String plus(String num1, String num2) {
StringBuilder sb = new StringBuilder();
int index1 = num1.length() - 1;
int index2 = num2.length() - 1;
int carry = 0;
while (index1 >= 0 || index2 >= 0 || carry > 0) {
int value1 = index1 >= 0 ? num1.charAt(index1) - '0' : 0;
int value2 = index2 >= 0 ? num2.charAt(index2) - '0' : 0;
int valueSum = value1 + value2 + carry;
carry = valueSum/10;
valueSum %= 10;
sb.append(valueSum);
index1--;
index2--;
}
return sb.reverse().toString();
}
private static String substract(String num1, String num2) {
StringBuilder sb = new StringBuilder();
boolean negtive = false;
if (compare(num1, num2) < 0) {
negtive = true;
String temp = num1;
num1 = num2;
num2 = temp;
}
int index1 = num1.length() - 1;
int index2 = num2.length() - 1;
int carry = 0;
while (index1 >= 0 || index2 >= 0) {
int value1 = index1 >= 0 ? num1.charAt(index1) - '0' : 0;
int value2 = index2 >= 0 ? num2.charAt(index2) - '0' : 0;
int valueSum = value1 - carry - value2;
carry = 0;
if (valueSum < 0) {
valueSum += 10;
carry = 1;
}
sb.append(valueSum);
index1--;
index2--;
}
int firstNonZero = sb.length()-1;
while (firstNonZero > 0 && sb.charAt(firstNonZero) == '0') {
firstNonZero--;
}
sb = new StringBuilder(sb.substring(0, firstNonZero+1));
if (negtive && !(sb.length() == 1 && sb.charAt(0) == '0')) {
sb.append('-');
}
return sb.reverse().toString();
}
private static int compare(String num1, String num2) {
if (num1.length() != num2.length()) {
return num1.length() - num2.length();
} else {
for (int idx = 0; idx < num1.length(); idx++) {
if (num1.charAt(idx) == num2.charAt(idx)) continue;
return num1.charAt(idx) - num2.charAt(idx);
}
}
return 0;
}
}
C版:
#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-fa
b[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); }