package test_wanglei;public class wang { public int[] a1;
public int[] a2;
public int[][] a3;
public int[] a4; public int[][] B1(int[] a1, int[] a2) {
for (int i = a1.length-1; i >= 1; i--) {
for (int j = a2.length-1; j >= 1; j--) {
int temp = a1[i] * a2[j];
if(temp >9){
a3[i][j-1] = a1[i] * a2[j - 1] + temp/10;
a3[i][j] = temp %10;
}
else a3[i][j] = temp;
}
}
return a3;
}
public int [] B2(int [][] a3){
for(int a = a3.length-1; a >=1; a--){
for(int b = a3.length-1; b >= 1; b--){
int add = 0;
add += a3[a][b];
if(add >9){
a4[a] = add % 10;
a4[a-1] = a4[a-1] + add/10;
}
else a4[a] = add;
}
}
return a4;
} public static void main(String[] args) {
wang w = new wang();
int [] x= {1,2};
int [] y= {1,2};
int [][] c = w.B1(x, y);
int [] d = w.B2(c);
System.out.println("=====================");
for(int x1 = 1; x1 < d.length; x1++){
System.out.println(d[x1]);
}
}}
public int[] a2;
public int[][] a3;
public int[] a4; public int[][] B1(int[] a1, int[] a2) {
for (int i = a1.length-1; i >= 1; i--) {
for (int j = a2.length-1; j >= 1; j--) {
int temp = a1[i] * a2[j];
if(temp >9){
a3[i][j-1] = a1[i] * a2[j - 1] + temp/10;
a3[i][j] = temp %10;
}
else a3[i][j] = temp;
}
}
return a3;
}
public int [] B2(int [][] a3){
for(int a = a3.length-1; a >=1; a--){
for(int b = a3.length-1; b >= 1; b--){
int add = 0;
add += a3[a][b];
if(add >9){
a4[a] = add % 10;
a4[a-1] = a4[a-1] + add/10;
}
else a4[a] = add;
}
}
return a4;
} public static void main(String[] args) {
wang w = new wang();
int [] x= {1,2};
int [] y= {1,2};
int [][] c = w.B1(x, y);
int [] d = w.B2(c);
System.out.println("=====================");
for(int x1 = 1; x1 < d.length; x1++){
System.out.println(d[x1]);
}
}}
a3 = new int[a1.length][a2.length];
for (int i = a1.length-1; i >= 1; i--) {
for (int j = a2.length-1; j >= 1; j--) {
int temp = a1[i] * a2[j];
if(temp >9){
a3[i][j-1] = a1[i] * a2[j - 1] + temp/10;
a3[i][j] = temp %10;
}
else a3[i][j] = temp;
}
}
return a3;
} public int [] B2(int [][] a3){
a4 = new int[a3.length];
for(int a = a3.length-1; a >=1; a--){
for(int b = a3.length-1; b >= 1; b--){
int add = 0;
add += a3[a][b];
if(add >9){
a4[a] = add % 10;
a4[a-1] = a4[a-1] + add/10;
}
else a4[a] = add;
}
} return a4;
} // 加上这两行就不报错了
因为每位相乘过后还有相错的位呢,如何解决呢
a3 = new int[a1.length][a2.length];
for (int i = a1.length-1; i >= 0; i--) {
for (int j = a2.length-1; j >= 0; j--) {
int temp = a1[i] * a2[j];
System.out.println("a1["+i+"] * a2["+j+"] = " + temp);
if(temp > 9){
a3[i][j-1] = a1[i] * a2[j - 1] + temp/10;
a3[i][j] = temp %10;
}
else a3[i][j] = temp;
}
}
return a3;
}
public class wang
{ private int[] a;
private int[] b;
public wang(int[] a, int[] b)
{
this.a = a;
this.b = b;
}
public int[] getMultipleResult()
{
//判断参数是否为空
if(a == null || b == null)
{
throw new IllegalArgumentException("Nonillable parameter is null!");
}
//判断参数是否合法
for(int temp : a)
{
if(temp > 9)
{
throw new IllegalArgumentException("Array item must less than 9!");
}
}
for(int temp : b)
{
if(temp > 9)
{
throw new IllegalArgumentException("Array item must less than 9!");
}
}
//创建结果数组,两个数相乘,结果的积最大为两个乘数位数之和
int[] resultTemp = new int[a.length + b.length];
//模拟手乘竖式
// 1 2 3 --内层循环
// * 4 5 6 --外层循环
// -------------
for (int i = a.length - 1; i >= 0; i--)
{
for (int j = b.length - 1; j >= 0; j--)
{
int temp = a[i] * b[j];
resultTemp[i + j + 1] += temp;
//处理进位
if(resultTemp[i + j + 1] > 9)
{
int carry = resultTemp[i + j + 1];
resultTemp[i + j + 1] %= 10;
resultTemp[i + j] += (int)(carry / 10);
}
}
}
int[] result = null;
//找到第一个不为零的元素
for (int i = 0; i < resultTemp.length; i++)
{
if(resultTemp[i] != 0)
{
result = new int[resultTemp.length - i];
System.arraycopy(resultTemp, i, result, 0, result.length);
break;
}
}
return result;
}
public static void main(String[] args)
{
int[] x = { 9, 9, 9}; int[] y = { 9, 9, 9};
wang w = new wang(x, y);
int[] d = w.getMultipleResult();
System.out.println("====================="); for (int i = 0; i < d.length; i++)
{
System.out.print(d[i]);
}
}
}