解法一:模拟
加法运算:进行归并操作,对于都有的指数项将系数求和,仅有一边有的项保持原样。
乘法运算:遍历两个数组,指数项相加,系数项相乘。由于遍历完无法保证按指数降序排列,因此要存储起来进行排序。
注意系数为0的项无效,不用输出,但是没有至少一个有效项的话要输出“0 0”。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
// 读入数据
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String[] strs = reader.readLine().split(" +");
int lenA = Integer.parseInt(strs[0]);
Item[] A = new Item[lenA];
for (int i = 1; i <= lenA; ++i) {
A[i - 1] = new Item(
Integer.parseInt(strs[(i << 1) - 1]),
Integer.parseInt(strs[(i << 1)])
);
}
strs = reader.readLine().split(" +");
int lenB = Integer.parseInt(strs[0]);
Item[] B = new Item[lenB];
for (int i = 1; i <= lenB; ++i) {
B[i - 1] = new Item(
Integer.parseInt(strs[(i << 1) - 1]),
Integer.parseInt(strs[(i << 1)])
);
}
// 乘法
mul(A, B);
System.out.println();
// 加法
sum(A, B);
}
private static void sum(Item[] A, Item[] B) {
int lenA = A.length;
int lenB = B.length;
int cnt = 0;
boolean flag;
int coef = 0, index = 0;
int p1 = 0, p2 = 0;
// 归并
while ((p1 < lenA) && (p2 < lenB)) {
flag = false;
if (A[p1].index > B[p2].index) {
flag = true;
index = A[p1].index;
coef = A[p1].coef;
++p1;
} else if (A[p1].index < B[p2].index) {
flag = true;
index = B[p2].index;
coef = B[p2].coef;
++p2;
} else {
coef = A[p1].coef + B[p2].coef;
if (coef != 0) {
flag = true;
index = A[p1].index;
}
++p1;
++p2;
}
// 本项有效
if (flag) {
++cnt;
if (cnt == 1) {
System.out.print(coef + " " + index);
} else {
System.out.print(" " + coef + " " + index);
}
}
}
if (p1 == lenA) {
while (p2 < lenB) {
index = B[p2].index;
coef = B[p2].coef;
++p2;
++cnt;
if (cnt == 1) {
System.out.print(coef + " " + index);
} else {
System.out.print(" " + coef + " " + index);
}
}
}
if (p2 == lenB) {
while (p1 < lenA) {
index = A[p1].index;
coef = A[p1].coef;
++p1;
++cnt;
if (cnt == 1) {
System.out.print(coef + " " + index);
} else {
System.out.print(" " + coef + " " + index);
}
}
}
if (cnt == 0) {
System.out.print("0 0");
}
}
private static void mul(Item[] A, Item[] B) {
Map<Integer, Integer> map = new HashMap<>();
int coef, index;
for (Item i : A) {
for (Item j : B) {
index = i.index + j.index;
coef = i.coef * j.coef;
map.put(index, coef + map.getOrDefault(index, 0));
}
}
List<int[]> ans = new ArrayList<>(map.size());
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
coef = entry.getValue();
if (coef != 0) {
index = entry.getKey();
ans.add(new int[]{coef, index});
}
}
if (ans.size() == 0) {
System.out.print("0 0");
} else {
ans.sort(new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return -Integer.compare(o1[1], o2[1]);
}
});
System.out.print(ans.get(0)[0] + " " + ans.get(0)[1]);
for (int i = 1; i < ans.size(); ++i) {
System.out.print(" " + ans.get(i)[0] + " " + ans.get(i)[1]);
}
}
}
}
class Item {
// 系数
int coef;
// 指数
int index;
public Item(int coef, int index) {
this.coef = coef;
this.index = index;
}
@Override
public String toString() {
return "Item{" +
"coef=" + coef +
", index=" + index +
'}';
}
}