解法一:模拟

加法运算:进行归并操作,对于都有的指数项将系数求和,仅有一边有的项保持原样。
乘法运算:遍历两个数组,指数项相加,系数项相乘。由于遍历完无法保证按指数降序排列,因此要存储起来进行排序。
注意系数为0的项无效,不用输出,但是没有至少一个有效项的话要输出“0 0”。

  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.*;
  5. public class Main {
  6. public static void main(String[] args) throws IOException {
  7. // 读入数据
  8. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  9. String[] strs = reader.readLine().split(" +");
  10. int lenA = Integer.parseInt(strs[0]);
  11. Item[] A = new Item[lenA];
  12. for (int i = 1; i <= lenA; ++i) {
  13. A[i - 1] = new Item(
  14. Integer.parseInt(strs[(i << 1) - 1]),
  15. Integer.parseInt(strs[(i << 1)])
  16. );
  17. }
  18. strs = reader.readLine().split(" +");
  19. int lenB = Integer.parseInt(strs[0]);
  20. Item[] B = new Item[lenB];
  21. for (int i = 1; i <= lenB; ++i) {
  22. B[i - 1] = new Item(
  23. Integer.parseInt(strs[(i << 1) - 1]),
  24. Integer.parseInt(strs[(i << 1)])
  25. );
  26. }
  27. // 乘法
  28. mul(A, B);
  29. System.out.println();
  30. // 加法
  31. sum(A, B);
  32. }
  33. private static void sum(Item[] A, Item[] B) {
  34. int lenA = A.length;
  35. int lenB = B.length;
  36. int cnt = 0;
  37. boolean flag;
  38. int coef = 0, index = 0;
  39. int p1 = 0, p2 = 0;
  40. // 归并
  41. while ((p1 < lenA) && (p2 < lenB)) {
  42. flag = false;
  43. if (A[p1].index > B[p2].index) {
  44. flag = true;
  45. index = A[p1].index;
  46. coef = A[p1].coef;
  47. ++p1;
  48. } else if (A[p1].index < B[p2].index) {
  49. flag = true;
  50. index = B[p2].index;
  51. coef = B[p2].coef;
  52. ++p2;
  53. } else {
  54. coef = A[p1].coef + B[p2].coef;
  55. if (coef != 0) {
  56. flag = true;
  57. index = A[p1].index;
  58. }
  59. ++p1;
  60. ++p2;
  61. }
  62. // 本项有效
  63. if (flag) {
  64. ++cnt;
  65. if (cnt == 1) {
  66. System.out.print(coef + " " + index);
  67. } else {
  68. System.out.print(" " + coef + " " + index);
  69. }
  70. }
  71. }
  72. if (p1 == lenA) {
  73. while (p2 < lenB) {
  74. index = B[p2].index;
  75. coef = B[p2].coef;
  76. ++p2;
  77. ++cnt;
  78. if (cnt == 1) {
  79. System.out.print(coef + " " + index);
  80. } else {
  81. System.out.print(" " + coef + " " + index);
  82. }
  83. }
  84. }
  85. if (p2 == lenB) {
  86. while (p1 < lenA) {
  87. index = A[p1].index;
  88. coef = A[p1].coef;
  89. ++p1;
  90. ++cnt;
  91. if (cnt == 1) {
  92. System.out.print(coef + " " + index);
  93. } else {
  94. System.out.print(" " + coef + " " + index);
  95. }
  96. }
  97. }
  98. if (cnt == 0) {
  99. System.out.print("0 0");
  100. }
  101. }
  102. private static void mul(Item[] A, Item[] B) {
  103. Map<Integer, Integer> map = new HashMap<>();
  104. int coef, index;
  105. for (Item i : A) {
  106. for (Item j : B) {
  107. index = i.index + j.index;
  108. coef = i.coef * j.coef;
  109. map.put(index, coef + map.getOrDefault(index, 0));
  110. }
  111. }
  112. List<int[]> ans = new ArrayList<>(map.size());
  113. for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
  114. coef = entry.getValue();
  115. if (coef != 0) {
  116. index = entry.getKey();
  117. ans.add(new int[]{coef, index});
  118. }
  119. }
  120. if (ans.size() == 0) {
  121. System.out.print("0 0");
  122. } else {
  123. ans.sort(new Comparator<int[]>() {
  124. @Override
  125. public int compare(int[] o1, int[] o2) {
  126. return -Integer.compare(o1[1], o2[1]);
  127. }
  128. });
  129. System.out.print(ans.get(0)[0] + " " + ans.get(0)[1]);
  130. for (int i = 1; i < ans.size(); ++i) {
  131. System.out.print(" " + ans.get(i)[0] + " " + ans.get(i)[1]);
  132. }
  133. }
  134. }
  135. }
  136. class Item {
  137. // 系数
  138. int coef;
  139. // 指数
  140. int index;
  141. public Item(int coef, int index) {
  142. this.coef = coef;
  143. this.index = index;
  144. }
  145. @Override
  146. public String toString() {
  147. return "Item{" +
  148. "coef=" + coef +
  149. ", index=" + index +
  150. '}';
  151. }
  152. }