• 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))+" ");
                }
            }
        }
    }
  • 相关阅读:
    关于enum ,调用webservice,用户控件与主页面之间的交互,datsource属性,net面试题,反射类生成sql语句,URl重写一个小实例
    一个很简单的图片上传后立即显示在页面的控件(c#)
    委托之实现异步调用
    跟我学Linq
    w3c关于sql sever的基础操作
    join操作基础
    表操作基础
    javascript理论篇(详情见地址)
    android universal-image-loader的使用
    json相关类库,java对象与json相互转换
  • 原文地址:https://www.cnblogs.com/godoforange/p/10909357.html
Copyright © 2020-2023  润新知