PAT1009:Product of Polynomials

Posted 0kk470

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT1009:Product of Polynomials相关的知识,希望对你有一定的参考价值。

1009. Product of Polynomials (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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.用一个vector poly存放系数乘积的结果,它的下标就是x的幂次方。两个多项式相乘后的最大次幂不会超过2000(因为Ni <= 1000)。

2.用一个countx统计不同次幂的个数,如果是第一次相加不为0,那countx加1。如果系数相加为0.那么countx就得减1,

3.输出countx,然后倒序输出poly中输出系数不为0的x幂次方就行。

代码
#include<iostream>
#include<vector>
#include<iomanip>
using namespace std;
vector<double> poly(2001,0);

class x
{
public:
    int exp;
    double co;
};

int main()
{
   vector<x> fst,scd;
   int k1;
   cin >> k1;
   for(int i = 0;i < k1;i++)
   {
       x tmp;
       cin >> tmp.exp >> tmp.co;
       fst.push_back(tmp);
   }
   int k2;
   cin >> k2;
   for(int i = 0;i < k2;i++)
   {
       x tmp;
       cin >> tmp.exp >> tmp.co;
       scd.push_back(tmp);
   }

   int countx = 0;
   for(int i = 0;i < fst.size();i++)
   {
       for(int j = 0;j < scd.size();j++)
       {
           int index = fst[i].exp + scd[j].exp;
           double res = fst[i].co * scd[j].co;
           if(poly[index] == 0)
             countx++;
           poly[index] += res;
           if(poly[index] == 0)
             countx--;
       }
   }

   cout << countx;
   for(int i = poly.size() - 1;i >= 0;i--)
   {
      if(poly[i] != 0)
        cout << " " << i << " " << fixed << setprecision(1) << poly[i];
   }
   cout << endl;
}

  

以上是关于PAT1009:Product of Polynomials的主要内容,如果未能解决你的问题,请参考以下文章

PAT甲级——A1009 Product of Polynomials

PAT A1009 Product of Polynomials (25 分)

PAT Advanced 1009 Product of Polynomials (25分)

PAT-A 1009. Product of Polynomials

PAT 甲级测试题目 -- 1009 Product of Polynomials

PAT甲题题解-1009. Product of Polynomials (25)-多项式相乘