PAT-A 1009. Product of Polynomials

参考:https://www.jianshu.com/p/e7a3ee0f82d9

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int N=1e6+5;
 4 double xi[N];
 5 struct node
 6 {
 7     int zhi;//指数
 8     double xi;//系数
 9 }noa[20],nob[20];
10 int main()
11 {
12     int na,nb;
13     cin>>na;
14     for (int i=0;i<na;i++)
15     {
16         cin>>noa[i].zhi;
17         cin>>noa[i].xi;
18     }
19     cin>>nb;
20     for (int i=0;i<nb;i++)
21     {
22         cin>>nob[i].zhi;
23         cin>>nob[i].xi;
24     }
25     double tempxi;//临时系数
26     int tempzhi,nc=0;//临时指数
27     for (int i=0;i<na;i++)
28     {
29         for (int j=0;j<nb;j++)
30         {
31             tempzhi=noa[i].zhi+nob[j].zhi;
32             tempxi=noa[i].xi*nob[j].xi;
33 //            if (xi[tempzhi]==(double)0)//在计算过程中,某一指数项的系数有可能暂时为0后又不为0,所以不能用
34 //            {                          //这段代码来计数!!
35 //                nc++;
36 //            }
37             xi[tempzhi]+=tempxi;
38         }
39     }
40     for (int i=N;i>=0;i--)
41     {
42         if (xi[i]!=(double)0)
43         {
44             nc++;
45         }
46     }
47     cout<<nc;
48     for (int i=N;i>=0;i--)
49     {
50         if (xi[i]!=(double)0)
51         {
52             printf(" %d %.1f",i,xi[i]);
53         }
54     }
55     cout<<endl;
56 
57     return 0;
58 }

猜你喜欢

转载自www.cnblogs.com/hemeiwolong/p/10722941.html