两大数相乘

import java.util.Scanner;

public class Multiply{
	public static void multiply(char a[], int len1, char b[], int len2){
		int size = len1 + len2 + 3;
		int c[] = new int[size];
		for (int j = 0; j < len2; j++)
			for (int i = 0; i < len1; i++)
				c[i + j] += Integer.parseInt(String.valueOf(a[i])) * Integer.parseInt(String.valueOf(b[j]));
		int m = 0;
		for (m = 0; m < size; m++){
			int d = c[m] / 10;
			c[m] = c[m] % 10;
			if (d > 0)
				c[m + 1] += d;
		}
		for (m = size - 1; m >= 0;){
			if (c[m] > 0)
				break;
			m--;
		}
		for (int n = 0; n <= m; n++)
			System.out.print(c[m - n]);
	}
	public static void main(String[] args){
		Scanner a = new Scanner(System.in);
		String str1 = a.next();
		String str2 = a.next();
		a.close();
		int len1 = str1.length();
		int len2 = str2.length();
		char s1[] = str1.toCharArray();
		char s2[] = str2.toCharArray();
		str1 = new StringBuffer(str1).reverse().toString();
		str2 = new StringBuffer(str2).reverse().toString();//将字符串反向
		System.out.println(str1 + "*" + str2);
		System.out.print("=");
		multiply(s1, len1, s2, len2);
	}
}

你可能感兴趣的:(两大数相乘)