直接把系数加1000映射到数组下标就行
求两个多项式的和
https://www.nowcoder.com/practice/a9901ba9163549a590d1d1c245f14d2a
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] nums1 = new int[2000]; int[] nums2 = new int[2000]; int m = sc.nextInt(); while (m-- > 0) { int a = sc.nextInt(); int b = sc.nextInt(); nums1[b + 1000] = a; } int n = sc.nextInt(); while (n-- > 0) { int a = sc.nextInt(); int b = sc.nextInt(); nums2[b + 1000] = a; } for (int i = 1999; i >= 0; i--) { int num = nums1[i] + nums2[i]; if (num != 0) System.out.print(num + " " + (i - 1000) + " "); } } }