PAT (Advanced Level) 1009 Product of Polynomials (25 分)
程序员文章站
2022-07-15 13:26:01
...
题目概述:
求解两个多项式之积
分析:
1.用map记录对应的项和系数
2.遍历mp1和mp2,使得他们每个项数之和的系数加上他们的系数之积,并将结果存入mp3中
3.忽略掉系数为0的项,求总项数
4.反向遍历,输出结果(忽略0项)
#include<bits/stdc++.h>
using namespace std;
map<int, double> mp1;
map<int, double> mp2;
map<int, double> mp3;
int main()
{
int k1, k2;
int a;
double b;
cin >> k1;
for(int i = 0; i < k1; i++)
{
cin >> a >> b;
mp1[a] = b;
}
cin >> k2;
for(int i = 0; i < k2; i++)
{
cin >> a >> b;
mp2[a] = b;
}
for(auto it1 = mp1.begin(); it1 != mp1.end(); it1++)
{
for(auto it2 = mp2.begin(); it2 != mp2.end(); it2++)
{
mp3[it1->first + it2->first] += it1->second * it2->second;
}
}
int cnt = 0;
for(auto it = mp3.begin(); it != mp3.end(); it++)
{
if(it->second != 0) cnt++;
}
cout << cnt;
for(auto it = mp3.rbegin(); it != mp3.rend(); it++)
{
if(it->second == 0) continue;
printf(" %d %.1f", it->first, it->second);
}
cout << endl;
return 0;
}
总结:
1.注意rbegin和rend用法
2.注意mp的第二项为double
推荐阅读
-
PAT (Advanced Level) 1012 The Best Rank (25 分)
-
PAT (Advanced Level) Practice - 1122 Hamiltonian Cycle(25 分)
-
PAT (Advanced Level) 1009 Product of Polynomials (25 分)
-
PAT (Advanced Level) 1006 Sign In and Sign Out (25 分)
-
【测试点0分析】1009 Product of Polynomials (25 分)
-
PAT (Advanced Level) Practice 1146 Topological Order (25分) (拓扑序列的判断)
-
PAT (Advanced Level) Practice - 1033 To Fill or Not to Fill(25 分)