【pta】7-2 一元多项式的乘法与加法运算 (20 分) <水题>
一、题目大意
题目链接:https://pintia.cn/problem-sets/15/problems/710
设计函数分别求两个一元多项式的乘积与和。
输入格式:
输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。
输出格式:
输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0。
输入样例:
4 3 4 -5 2 6 1 -2 0
3 5 20 -7 4 3 1
结尾无空行
输出样例:
15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1
5 20 -4 4 -5 2 9 1 -2 0
结尾无空行
二、wa代码
#include<bits/stdc++.h> using namespace std; int main(void) { map<int,int>mp1,mp2,mp3,mp4; int n1,n2; scanf("%d",&n1); for (int i=0;i<n1;i++) { int a,b; scanf("%d %d",&a,&b); mp1[b]=a; //getchar(); //getchar(); } scanf("%d",&n2); for (int i=0;i<n2;i++) { int a,b; scanf("%d %d",&a,&b); mp2[b]=a; //getchar(); //getchar(); } for (auto it1:mp1) { for (auto it2:mp2) { mp3[it1.first+it2.first]+=it1.second*it2.second; } } for (auto it=mp3.rbegin();it!=mp3.rend();it++) { if (it!=mp3.rbegin()) printf(" "); printf("%d %d",it->second,it->first); } if (mp3.size()==0) printf("0 0"); printf("\n"); for (auto it:mp1) { mp4[it.first]+=it.second; } for (auto it:mp2) { mp4[it.first]+=it.second; } //for (auto it:mp1) cout<<"mp1.first:"<<it.first<<endl; //for (auto it:mp2) cout<<"mp2.first:"<<it.first<<endl; //for (auto it:mp3) cout<<"mp3.first:"<<it.first<<endl; //for (auto it:mp4) cout<<"mp4.first:"<<it.first<<endl; for (auto it=mp4.rbegin();it!=mp4.rend();it++) { if (it!=mp4.rbegin()) printf(" "); printf("%d %d",it->second,it->first); } if (mp4.size()==0) printf("0 0"); return 0; }
这里我采用map来保存系数和指数,因为map自带排序,只要将指数作为键,系数作为值就能实现指数从小到大的排序
当时我有两个点一直过不了

后来参看了大佬写的题解
https://blog.csdn.net/qq_40935723/article/details/89182019
对其中合并同类项进行了更改,又过了一个点
#include<bits/stdc++.h> using namespace std; int main(void) { map<int,int>mp1,mp2,mp3,mp4; int n1,n2; scanf("%d",&n1); for (int i=0;i<n1;i++) { int a,b; scanf("%d %d",&a,&b); mp1[b]=a; } scanf("%d",&n2); for (int i=0;i<n2;i++) { int a,b; scanf("%d %d",&a,&b); mp2[b]=a; } for (auto it1:mp1) { for (auto it2:mp2) { mp3[it1.first+it2.first]+=it1.second*it2.second; } } for (auto it=mp3.rbegin();it!=mp3.rend();it++) { if (it->second==0) continue; if (it!=mp3.rbegin()) printf(" "); printf("%d %d",it->second,it->first); } if (mp3.size()==0) printf("0 0"); printf("\n"); for (auto it:mp1) { mp4[it.first]+=it.second; } for (auto it:mp2) { mp4[it.first]+=it.second; } //for (auto it:mp1) cout<<"mp1.first:"<<it.first<<endl; //for (auto it:mp2) cout<<"mp2.first:"<<it.first<<endl; //for (auto it:mp3) cout<<"mp3.first:"<<it.first<<endl; //for (auto it:mp4) cout<<"mp4.first:"<<it.first<<endl; for (auto it=mp4.rbegin();it!=mp4.rend();it++) { if (it->second==0) continue; if (it!=mp4.rbegin()) printf(" "); printf("%d %d",it->second,it->first); } if (mp4.size()==0) printf("0 0"); return 0; }

最后在测试的时候发现了问题

输出的格式有点问题,再次修改才ac
三、ac代码
#include<bits/stdc++.h> using namespace std; int main(void) { map<int,int>mp1,mp2,mp3,mp4; int n1,n2,cnt=0; scanf("%d",&n1); for (int i=0;i<n1;i++) { int a,b; scanf("%d %d",&a,&b); mp1[b]=a; } scanf("%d",&n2); for (int i=0;i<n2;i++) { int a,b; scanf("%d %d",&a,&b); mp2[b]=a; } for (auto it1:mp1) { for (auto it2:mp2) { mp3[it1.first+it2.first]+=it1.second*it2.second; } } for (auto it=mp3.rbegin();it!=mp3.rend();it++) { if (it->second==0) continue; if (cnt) printf(" "); printf("%d %d",it->second,it->first); cnt++; } if (cnt==0) printf("0 0"); printf("\n"); for (auto it:mp1) { mp4[it.first]+=it.second; } for (auto it:mp2) { mp4[it.first]+=it.second; } //for (auto it:mp1) cout<<"mp1.first:"<<it.first<<endl; //for (auto it:mp2) cout<<"mp2.first:"<<it.first<<endl; //for (auto it:mp3) cout<<"mp3.first:"<<it.first<<endl; //for (auto it:mp4) cout<<"mp4.first:"<<it.first<<endl; cnt=0; for (auto it=mp4.rbegin();it!=mp4.rend();it++) { if (it->second==0) continue; if (cnt) printf(" "); printf("%d %d",it->second,it->first); cnt++; } if (cnt==0) printf("0 0"); return 0; }

思路:
两个多项式相乘,即系数相乘,指数相加
两个多项式相加,若指数相同则系数相加
采用map可以很好的过滤相同指数
四、亿些知识点
题目比较水,主要细节比较多,知识点比较少
在使用map的时候,默认是键从小到大排列的
因此,在不改变默认排序的情况下,本题我采用了反向迭代器反过来遍历
for (auto it=mp4.rbegin();it!=mp4.rend();it++) //或者写成 for (map<int,int>::reverse_iterator it=mp4.rbegin();it!=mp4.rend();it++)


浙公网安备 33010602011771号