给定两个大数A和B ,任务是使用网格方法查找这两个数的乘积。
例子:
Input: A = 23, B = 15
Output: 345
Input: A = 321, B = 69
Output: 22149
方法:
- 创建N行和M列的2D数组,其中N是第一个数字的位数,M是第二个数字的位数。
- 将行的每个元素与列的每个元素相乘
- 对角线总数=行+列– 1
= 2 + 2 -1
= 3 - 创建一维数组,其中每个对角线都包含元素
d3 = 2
d2 = 13
d1 = 15对角线总和[] = {2,13,15}
输出=“”
总计= 0
我= DiagonalSum.length – 1 - 以相反的顺序重复插入,但对角和[]数组中的第一个元素除外
总数=总数+ DiagonalSum [i]。如果合计包含多个位数,则合计=总数中除单位位数以外的所有位数。输出= unit_place_digit +输出,否则总计= 0 - 总数=总数+对角线总和[0]
产出=总数+产出
下面是上述方法的实现:
// Java program to multiply Large
// numbers using the grid method
class GFG {
// Function to return the multiplication of a and b
public static String multiply(String a, String b)
{
boolean flag1 = false;
boolean flag2 = false;
a = a.trim();
b = b.trim();
// To check whether numbers are
// negative or positive
if (a.charAt(0) == '-') {
a = a.replace("-", "");
flag1 = true;
}
if (b.charAt(0) == '-') {
b = b.replace("-", "");
flag2 = true;
}
// To store the result of
// multiplication
String out = "";
// To create matrix(Grid) of row * column
int row = a.length();
int column = b.length();
int[][] c = new int[row][column];
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
int n1
= Character
.getNumericValue(
a.charAt(i));
int n2
= Character
.getNumericValue(
b.charAt(j));
c[i][j] = n1 * n2;
}
}
// To create 1D array of (row+column-1) size
// which is equal to total number
// of diagonal in matrix
int[] sum = new int[row + column - 1];
int m = 0;
// To add elements of each diagonals
for (int i = 0; i < row; i++) {
int k = i;
int add = 0;
for (int j = 0; j < column && k >= 0; j++, k--) {
add = add + c[k][j];
}
sum[m] = add;
m = m + 1;
}
for (int k = 1; k < column; k++) {
int i = row - 1;
int j = k;
int add = 0;
while (j < column && i >= 0) {
add = add + c[i][j];
j = j + 1;
i = i - 1;
}
sum[m] = add;
m = m + 1;
}
// To check both numbers are not
// single digit number
if (sum.length != 1) {
String temp
= Integer
.toString(
sum[sum.length - 1]);
int t = 0;
// Repeat element in "sum" Array
// in reverse order
for (int n = sum.length - 1; n >= 1; n--) {
// Add element with result "t"
t = t + sum[n];
// Convert integer element into String
// which is sum of all elements
// of particular diagonal
temp = Integer.toString(t);
if (temp.length() > 1) {
// If the number contains more than a single-digit
// then copy all the digit into "temp"
// as String except for the unit place digit
String str = temp.substring(0, temp.length() - 1);
t = Integer.parseInt(str);
}
else {
t = 0;
}
// Concat unit place digit at the
// beginning of String "out"
out = temp.charAt(temp.length() - 1) + out;
}
// Add first element with result "t"
t = t + sum[0];
temp = Integer.toString(t);
out = temp + out;
}
else {
out = out + sum[0];
}
StringBuffer s = new StringBuffer(out);
// To remove Zero's from the beginning
// of the multiplication result
for (int i = 0; i < s.length() - 1; i++) {
if (s.charAt(i) == '0') {
s.deleteCharAt(i);
i = i - 1;
}
else {
break;
}
}
out = s.toString();
// Check if the result of multiplication
// operation is zero
if (!out.equals("0")) {
// If one of two numbers is negative then
// assign minus sign to the result of
// multiplication operation
if (flag1 == true && flag2 == false) {
out = "-" + out;
}
else if (flag2 == true && flag1 == false) {
out = "-" + out;
}
}
return out;
}
// Driver code
public static void main(String args[])
{
String str1 = "123456789";
String str2 = "987654321";
System.out.println(multiply(str1, str2));
str1 = "1235421415454545454545454544";
str2 = "1714546546546545454544548544544545";
System.out.println(multiply(str1, str2));
}
}
输出:
121932631112635269
2118187521397235888154583183918321221520083884298838480662480