Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
7-2 univariate polynomial multiplication and addition operation (20 minutes)
Design of the sum of two functions are univariate polynomial and the product.
Input formats:
Input line 2 minutes, respectively, to each row number of the polynomial given non-zero entries, then descending exponential manner enter a nonzero polynomial coefficient and the exponent (integer not exceeding the absolute value of both 1000). Between numbers separated by a space.
Output formats:
Output in 2 rows, respectively descending exponential manner, and outputs the product polynomial and polynomial coefficients and non-zero entries in the index. Between numbers separated by spaces, but the end can not have extra spaces. Zero polynomial should be output 0 0
.
Sample input:
4 3 4 -5 2 6 1 -2 0
3 5 20 -7 4 3 1
Sample output:
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
#include<iostream>
#include <map>
using namespace std;
void input(map<int, int, greater<int>>& mp) {
int t, e, n;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> t >> e;
mp[e] = t;
}
}
void output(const map<int, int, greater<int>>& mp) {
bool flag = false;
for (auto& it : mp) {
if (it.second) {
cout << (flag ? " " : "") << it.second << " " << it.first ;
flag = true;
}
}
cout << (flag ? "" : "0 0") << endl;
}
int main(){
map<int, int,greater<int>> a, b, add, mult;
int n, flag = 0;
input(a); input(b);
add = a;
for (auto& it : b)
add[it.first] += it.second;
for (auto& i : a) {
for (auto& j : b) {
mult[i.first + j.first] += i.second * j.second;
}
}
output(mult); output(add);
return 0;
}