大数相乘 (JAVA版)


import java.util.Scanner;


public class BigMultiTest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner reader=new Scanner(System.in);
String numStr1=reader.next();
String numStr2=reader.next();


int[] intNum1=changetoArray(numStr1);
int[] intNum2=changetoArray(numStr2);

multiply(intNum1,intNum2);
}
public static int[] changetoArray(String numStr)
{
int length=numStr.length();
int[] intNum=new int[length];
for(int i=0;i<length;i++)
intNum[length-i-1]=Integer.parseInt(String.valueOf((numStr.charAt(i))));
return intNum;

}

public static int[] multiply(int[] num1,int[] num2)
{
int length1=num1.length;
int length2=num2.length;
int[] c=new int[length1+length2+1];
int[] result=new int[length1+length2];
for(int i=0;i<length1;i++)
for(int j=0;j<length2;j++)
{
int temp=num1[i]*num2[j]+c[i+j];
c[i+j]=0;
temp+=result[j+i];
result[i+j]=temp%10;
c[i+j+1]=temp/10;
}

StringBuffer sb=new StringBuffer();
for(int i=result.length-2;i>=0;i--)
sb.append(result[i]);
System.out.println(sb.toString());
return result;
}
}

你可能感兴趣的:(java)