zoukankan      html  css  js  c++  java
  • 1009 Product of Polynomials

    This time, you are supposed to find A×B where A and B are two polynomials.

    Input Specification:

    Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:

    N1​​ aN1​​​​ N2​​ aN2​​​​ ... NK​​ aNK​​​​

    where K is the number of nonzero terms in the polynomial, Ni​​ and aNi​​​​ (,) are the exponents and coefficients, respectively. It is given that 1, 0.

    Output Specification:

    For each test case you should output the product of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate up to 1 decimal place.

    Sample Input:

    2 1 2.4 0 3.2
    2 2 1.5 1 0.5
    

    Sample Output:

    3 3 3.6 2 6.0 1 1.6

    注意!如果系数为0则必须清除!!!这也是0测试点不通过的原因。
    import java.util.*;
    
    public class Main{
        static Map<Integer,Double> Map1;
        static Map<Integer,Double> Map2;
        static Map<Integer,Double> Map3;
        static int[] index1;
        static int[] index2;
        static int Max = 0;
        static int Min = 10000;
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            Map1 = new HashMap<>();
            Map2 = new HashMap<>();
            Map3 = new HashMap<>();
            int K = sc.nextInt();
            for(int i=0;i<K;i++) {
                Map1.put(sc.nextInt(),sc.nextDouble());
            }
            K = sc.nextInt();
            for(int i=0;i<K;i++) {
                Map2.put(sc.nextInt(),sc.nextDouble());
            }
            sc.close();
            Iterator<Integer> it= Map1.keySet().iterator();
            while(it.hasNext()) {
                int now1 = it.next();
                Iterator<Integer> it2 = Map2.keySet().iterator();
                while(it2.hasNext()) {
                    int now2 = it2.next();
                    int now3 = now1+now2;
                    if(now3>Max) Max=now3;
                    if(now3<Min) Min =now3;
                    Double res = Map1.get(now1)*Map2.get(now2);
                    if(Map3.containsKey(now3)) {
                        res+=Map3.get(now3);
                        Map3.put(now3,res);
                        if(Map3.get(now3)==0) Map3.remove(now3);
                    }else {
                        Map3.put(now3,res);
                        if(Map3.get(now3)==0) Map3.remove(now3);
                    }
                }
            }
            System.out.print(Map3.size()+" ");
            for(int i=Max;i>Min-1;i--) {
                if(Map3.containsKey(i)) {
                    if(i==Min)
                        System.out.print(i+" "+String.format("%.1f",Map3.get(i)));
                    else
                        System.out.print(i+" "+String.format("%.1f",Map3.get(i))+" ");
                }
            }
        }
    }
  • 相关阅读:
    opencv-活体检测
    人脸识别
    Opencv-python基本操作
    白话深度学习与Tensorflow(二)
    Linux系统入门(一)-未完成
    编程题29 题目:求对角线元素之和
    编程题28 题目 排序
    编程题27 题目:求100之内的素数
    编程题 18兵乓球比赛
    编程题21 求阶数总和
  • 原文地址:https://www.cnblogs.com/godoforange/p/10909357.html
Copyright © 2011-2022 走看看