欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

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

发布时间:2025/6/17 编程问答 59 豆豆
生活随笔 收集整理的这篇文章主要介绍了 7-2一元多项式的乘法与加法运算 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

title: "7-2一元多项式的乘法与加法运算(20"
date: 2018-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; }

转载于:https://www.cnblogs.com/lepeCoder/p/9181064.html

总结

以上是生活随笔为你收集整理的7-2一元多项式的乘法与加法运算的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。