直接把系数加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) + " ");
        }
    }
}

全部评论

相关推荐

本神尊:看来是没招到小红薯上的人
点赞 评论 收藏
分享
点赞 评论 收藏
分享
06-12 16:23
已编辑
小米_软件开发(准入职员工)
点赞 评论 收藏
分享
06-05 19:46
已编辑
武汉大学 后端
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务