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:
K 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))+" "); } } } }
微信搜索桔子科研或者扫描二维码,第一时间获取编程有趣的知识和最新科研学术成果。
![](https://images.cnblogs.com/cnblogs_com/godoforange/1390071/o_211204035711_qrcode_for_gh_7f100002de19_344.jpg)