1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 7-2一元多项式的乘法与加法运算

7-2一元多项式的乘法与加法运算

时间:2023-05-02 03:35:44

相关推荐

7-2一元多项式的乘法与加法运算

title: "7-2一元多项式的乘法与加法运算(20"

date: -06-14T01:09:46+08:00

tags: [""]

categories: ["PTA"]

7-2 一元多项式的乘法与加法运算(20 分)

设计函数分别求两个一元多项式的乘积与和。

输入格式:

输入分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

代码

#include <bits/stdc++.h>using namespace std;int main(){vector<pair<int, int>> a, b;int an, bn, i;cin >> an;pair<int, int> t;for(i = 0; i < an; i++) {cin >> t.second;//系数cin >> t.first;//指数a.emplace_back(t);}cin >> bn;for(i = 0; i < bn; i++) {cin >> t.second;cin >> t.first;b.emplace_back(t);}//计算乘法map<int, int> m2;for(auto va : a) {for(auto vb : b) {m2[va.first + vb.first] += (va.second * vb.second);}}bool b2 = false;for(auto m = m2.rbegin(); m != m2.rend()--; m++) {if((*m).second != 0) {if(b2 == false) {cout << (*m).second << ' ' << (*m).first;b2 = true;} else {cout << ' ' << (*m).second << ' ' << (*m).first;}}}if(!b2) {cout << "0 0";}cout << endl;//计算加法map<int, int> m1; //默认为0for(auto v : a) {m1[v.first] += v.second;}for(auto v : b) {m1[v.first] += v.second;}bool b1 = false;for(auto m = m1.rbegin(); m != m1.rend(); m++) {if((*m).second != 0) {if(b1 == false) {cout << (*m).second << ' ' << (*m).first;b1 = true;} else {cout << ' ' << (*m).second << ' ' << (*m).first;}}}if(!b1) {cout << "0 0";}return 0;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。