package com.test;
public class Multiply
{
public static void multiply(char a[], char b[])
{
int alen=a.length;
int blen=b.length;
int[] result = new int[alen + blen];
for (int i = 0; i < alen + blen; i++)
result[i] = 0;
int resultCount=blen+alen-2;
// 对齐逐位相乘
for (int j = blen-1; j >= 0; j--)
{
int tempb=(int)(b[j]-'0');
for (int i =alen - 1; i >= 0; i--)
{
int tempa=(int)(a[i]-'0');
result[i + j + 1] += tempa * tempb;
int carry = result[i+j+1] / 10;
result[i+j+1] = result[i+j+1] % 10;
if (carry > 0)
{
result[i+j] += carry;
}
}
}
if(result[0]>0)
{
resultCount+=1;
}
System.out.print("乘积:");
for (int j=0;j <=resultCount; j++) {
System.out.print(result[j]);
}
}
public static void main(String[] args) {
//String str1 = "999";
//String str2 = "999";
String str1 = "23456789009877666555544444";
String str2 = "34658743659843759437594387594387";
char[] s1 = str1.toCharArray();
char[] s2 = str2.toCharArray();
System.out.println("乘数1:"+str1);
System.out.println("乘数2:"+str2);
multiply(s1, s2);
}
}
public class Multiply
{
public static void multiply(char a[], char b[])
{
int alen=a.length;
int blen=b.length;
int[] result = new int[alen + blen];
for (int i = 0; i < alen + blen; i++)
result[i] = 0;
int resultCount=blen+alen-2;
// 对齐逐位相乘
for (int j = blen-1; j >= 0; j--)
{
int tempb=(int)(b[j]-'0');
for (int i =alen - 1; i >= 0; i--)
{
int tempa=(int)(a[i]-'0');
result[i + j + 1] += tempa * tempb;
int carry = result[i+j+1] / 10;
result[i+j+1] = result[i+j+1] % 10;
if (carry > 0)
{
result[i+j] += carry;
}
}
}
if(result[0]>0)
{
resultCount+=1;
}
System.out.print("乘积:");
for (int j=0;j <=resultCount; j++) {
System.out.print(result[j]);
}
}
public static void main(String[] args) {
//String str1 = "999";
//String str2 = "999";
String str1 = "23456789009877666555544444";
String str2 = "34658743659843759437594387594387";
char[] s1 = str1.toCharArray();
char[] s2 = str2.toCharArray();
System.out.println("乘数1:"+str1);
System.out.println("乘数2:"+str2);
multiply(s1, s2);
}
}