A1009. Product of Polynomials (25)

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 (i=1, 2, ..., K) are the exponents and coefficients, respectively. It is given that 1 <= K <= 10, 0 <= NK < ... < N2 < N1 <=1000.

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
 1 // hahaha.cpp : 定义控制台应用程序的入口点。
 2 //
 3 
 4 #include <stdafx.h>
 5 #include <stdio.h>
 6 #include <iostream>
 7 #include <vector>
 8 #include <map>
 9 #include <string>
10 #include <cstdio>
11 #include <set>
12 #include <algorithm>
13 #include <string.h>
14 using namespace std;
15 
16 const int maxn=2010;
17 double a[maxn]={0};
18 double b[maxn]={0};
19 double c[maxn]={0};
20 
21 int main()
22 {
23     int n;
24     scanf("%d",&n);
25     for(int i=0;i<n;i++)
26         {
27         int p;
28         double q;
29         scanf("%d %lf",&p,&q);
30         a[p]=q;
31         }
32     scanf("%d",&n);
33     for(int i=0;i<n;i++)
34         {
35         int p;
36         double q;
37         scanf("%d %lf",&p,&q);
38         b[p]=q;
39         }
40     int count=0;
41     for(int i=0;i<maxn;i++)
42         {
43         for(int j=0;j<=i;j++)
44             {
45             c[i]=c[i]+a[j]*b[i-j];
46             }
47         if(c[i]!=0)count++;
48         }
49     printf("%d",count);
50     for(int i=maxn-1;i>=0;i--)
51           {
52              if(c[i]!=0)
53               {
54               printf(" %d %.1f",i,c[i]);
55               }
56           }
57 
58     return 0;
59 }
原文地址:https://www.cnblogs.com/ligen/p/4342691.html