7-2一元多项式的乘法与加法运算
生活随笔
收集整理的這篇文章主要介紹了
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一元多项式的乘法与加法运算的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (2.10)备份与还原--利用T-SQL
- 下一篇: 解决VS2013卡顿现象,很有用