题目链接:
多项式乘法,下面是代码:
1 //注意输出的格式 2 #include3 #include 4 #include 5 using namespace std; 6 struct pol{ 7 float xishu; 8 int zhishu; 9 friend bool operator < (pol x,pol y){10 return x.zhishu < y.zhishu;11 }12 friend void operator * (pol &x,pol &other){13 x.xishu *= other.xishu;14 x.zhishu += other.zhishu;15 }16 friend void operator +(pol &x,pol &other){17 x.xishu += other.xishu;18 }19 };20 21 int main(){22 priority_queue q1;23 priority_queue q2;24 priority_queue q3;25 priority_queue q4;26 pol p;27 int n,m;28 cin >> n;29 for(int i = 0; i < n; i++){30 cin >> p.zhishu >> p.xishu;31 q1.push(p);32 }33 cin >> m;34 for(int i = 0; i < m; i++){35 cin >> p.zhishu >> p.xishu;36 q2.push(p);37 }38 //q3 = q2;39 int n1 = q1.size();40 int n2 = q2.size();41 for(int i = 0 ; i < n1; i++){42 pol p1 = q1.top();43 44 q1.pop();45 for(int j = 0 ; j < n2; j++){46 p = p1;47 pol p2 = q2.top();48 // cout << p2.zhishu << " " << p2.xishu << endl;49 q2.pop();50 q3.push(p2);51 p*p2;52 // cout << p.zhishu << " " << p.xishu << endl;53 q4.push(p);54 }55 while(!q3.empty()){56 q2.push(q3.top());57 q3.pop();58 }59 }60 // for(int i = 0; i < q4.size(); i++){ 61 //// cout << q4.top().zhishu << " " << q4.top().xishu << endl;62 // q4.pop();63 // }64 p = q4.top();65 n1 = q4.size();66 q4.pop();67 for(int i = 0; i < n1-1; i++){68 pol p1 = q4.top();69 q4.pop();70 if(p.zhishu == p1.zhishu) {71 // cout << p.zhishu << " " << p1.zhishu << endl;72 p+p1;73 // cout << p.zhishu << " " << p.xishu << endl;74 75 }76 else{77 if(p.xishu != 0)78 q3.push(p);79 p = p1;80 // cout << p.zhishu << " " << p.xishu << endl;81 }82 }83 if(p.xishu != 0) q3.push(p);84 //q3.push(p);85 cout << q3.size() << " ";86 n1 = q3.size();87 for(int i = 0; i < n1; i++){88 if(i != n1-1){89 cout << q3.top().zhishu << " " << fixed << setprecision(1) << q3.top().xishu << " ";90 q3.pop();91 }92 else cout << q3.top().zhishu << " " << fixed << setprecision(1) << q3.top().xishu;93 }94 // cout << p.zhishu << " " << p.xishu;95 // cout << q3.size() << endl;96 // cout << q3.top().zhishu << " " << q3.top().xishu;97 return 0;98 }