PAT(Advanced) 1009 Product of Polynomials C++实现

PAT(Advanced)甲级 1009 Product of Polynomials C++实现

题目链接

PAT(Advanced) 1009 Product of Polynomials

题目大意

给定两个多项式,求出其乘积

算法思路

设置一个数组,下标作阶码,值为系数,逐个遍历,阶码累加,系数相乘即可!详情请看下述代码!

AC代码

#include <bits/stdc++.h>
using namespace std;
const int MAX_SIZE = 2000 + 10;
int main(int argc, char const *argv[])
{int K;float polynomials[MAX_SIZE] = {0};int maxExponent = -1;vector< pair<int, float> > v1, v2;cin >> K;v1.resize(K);for (int i = 0; i < K; i++) {cin >> v1[i].first >> v1[i].second;}cin >> K;v2.resize(K);for (int i = 0; i < K; i++) {cin >> v2[i].first >> v2[i].second;}for (int i = 0; i < v1.size(); i++) {for (int j = 0; j < v2.size(); j++) {polynomials[v1[i].first + v2[j].first] += v1[i].second * v2[j].second;maxExponent = max(maxExponent, v1[i].first + v2[j].first);}}int cnt = 0;for (int i = maxExponent; i >= 0; i--) {if (polynomials[i] != 0) {cnt++;}}printf("%d", cnt);for (int i = maxExponent; i >= 0; i--) {if (polynomials[i] != 0) {printf(" %d %.1f", i, polynomials[i]);}}return 0;
}

样例输入

2 1 2.4 0 3.2
2 2 1.5 1 0.5

样例输出

3 3 3.6 2 6.0 1 1.6

鸣谢

感谢PAT提供的题目及测评平台!

最后

本题为简单题,注意一些边界情况即可!

由于博主水平有限,不免有疏漏之处,欢迎读者随时批评指正,以免造成不必要的误解!

PAT(Advanced) 1009 Product of Polynomials C++实现

PAT(Advanced)甲级 1009 Product of Polynomials C++实现

题目链接

PAT(Advanced) 1009 Product of Polynomials

题目大意

给定两个多项式,求出其乘积

算法思路

设置一个数组,下标作阶码,值为系数,逐个遍历,阶码累加,系数相乘即可!详情请看下述代码!

AC代码

#include <bits/stdc++.h>
using namespace std;
const int MAX_SIZE = 2000 + 10;
int main(int argc, char const *argv[])
{int K;float polynomials[MAX_SIZE] = {0};int maxExponent = -1;vector< pair<int, float> > v1, v2;cin >> K;v1.resize(K);for (int i = 0; i < K; i++) {cin >> v1[i].first >> v1[i].second;}cin >> K;v2.resize(K);for (int i = 0; i < K; i++) {cin >> v2[i].first >> v2[i].second;}for (int i = 0; i < v1.size(); i++) {for (int j = 0; j < v2.size(); j++) {polynomials[v1[i].first + v2[j].first] += v1[i].second * v2[j].second;maxExponent = max(maxExponent, v1[i].first + v2[j].first);}}int cnt = 0;for (int i = maxExponent; i >= 0; i--) {if (polynomials[i] != 0) {cnt++;}}printf("%d", cnt);for (int i = maxExponent; i >= 0; i--) {if (polynomials[i] != 0) {printf(" %d %.1f", i, polynomials[i]);}}return 0;
}

样例输入

2 1 2.4 0 3.2
2 2 1.5 1 0.5

样例输出

3 3 3.6 2 6.0 1 1.6

鸣谢

感谢PAT提供的题目及测评平台!

最后

本题为简单题,注意一些边界情况即可!

由于博主水平有限,不免有疏漏之处,欢迎读者随时批评指正,以免造成不必要的误解!