JAVA高精度加法数组和Biginteger实现

import java.math.BigInteger;
import java.util.Scanner;

import javax.print.attribute.URISyntax;

public class 高精度加法 {
	public static void main(String[] args) {
		/*
		 * Scanner read=new Scanner(System.in); BigInteger a,b,c;
		 * a=read.nextBigInteger(); b=read.nextBigInteger(); c=a.add(b);
		 * System.out.println(c);
		 */   (biginteger 实现)
-----------------------------------------------------------------------------------------------------------------------------------------------              ------------------数组实现             ------------------------------------------------------
		Scanner read = new Scanner(System.in);
		char a[], b[];
		StringBuffer ta = new StringBuffer(read.next());
		StringBuffer tb = new StringBuffer(read.next());
		ta.reverse();
		tb.reverse();
		int alen = ta.length();
		int blen = tb.length();

		boolean istalonger = alen > blen ? true : false;
		for (int i = 0; i < Math.abs(alen - blen); i++) {
			if (istalonger)
				tb.append("0");
			else
				ta.append("0");
		}
		ta.append("0");
		tb.append("0");
		a = ta.toString().toCharArray();
		b = tb.toString().toCharArray();
		int maxl = a.length;
		int ia[] = new int[maxl];
		int ib[] = new int[maxl];
		int result[] = new int[maxl];
		for (int i = 0; i < maxl; i++) {
			ia[i] = Character.getNumericValue(a[i]);
			ib[i] = Character.getNumericValue(b[i]);
		}
		int temp = 0, s;
		for (int i = 0; i < maxl; i++) {
			s = ia[i] + ib[i] + temp;
			result[i] = s % 10;
			temp = s / 10;
		}
		int k = result.length - 1;
		for (; k >= 0; k--) {
			if (result[k] != 0)
				break;
		}
		for (int i =k; i >= 0; i--) {
			System.out.print(result[i]);
		}

	}
}


已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页