PAT 1009 Product of Polynomials (25)

来源:互联网 发布:开淘宝店到底怎么样 编辑:程序博客网 时间:2024/06/08 18:50

题目:

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.22 2 1.5 1 0.5
Sample Output
3 3 3.6 2 6.0 1 1.6
我的程序:
#include<iostream>//#include<cstdio>#include<iomanip>using namespace std;struct p{  int m;  double a;};int main(){  double mi[2001]={0};//  int n=2;  int k1,k2;  cin>>k1;  struct p *p1=new struct p [k1];  for(int i=0;i<k1;i++)  {    int x;    double y;    cin>>x>>y;    p1[i].m=x;    p1[i].a=y;  }  cin>>k2;  struct p* p2 = new struct p [k2];  for(int i=0;i<k2;i++)  {    int x;    double y;    cin>>x>>y;    p2[i].m=x;    p2[i].a = y;  }  int count=0;  for(int i=0;i<k1;i++)  {    for(int j=0;j<k2;j++)    {      int l=p1[i].m+p2[j].m;      double r=p1[i].a*p2[j].a;      mi[l] += r;    }  }  for(int i=0;i<2001;i++)  {    if(mi[i]!=0)    count++;  }  cout<<count;  for(int i=2000;i>=0;i--)  {    if(mi[i]!=0)    cout<<" "<<i<<" "<<fixed<<setprecision(1)<<mi[i];  }//  system("pause");  return 0;}


有什么错误还有什么可以优化的方法,非常欢迎大家指出!一起进步!

原创粉丝点击