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

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

输入格式:

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

 思路:map操作,直接遍历即可,开三个意思意思。vector逆序存储后面逆序一下再输出即可

#include<bits/stdc++.h>
using namespace std;

#define x first
#define y second
#define endl '\n'
#define rep(i,a,n) for (int i = a; i < n; i ++ )
#define repn(i,a,n) for (int i = a; i <= n; i ++ )
#define pb push_back
#define IOS ios::sync_with_stdio(false); cin.tie(0);cout.tie(0);
typedef long long ll;
typedef pair<int,int> PII;

ll gcd(ll a,ll b) { return b ? gcd(b,a % b) : a; }
const int mod = 1e9+7;
map<int, int> s1, s2, s3;
map<int, int> ans;


int main()
{
    IOS;
    int n;
    cin >> n;
    while(n -- )
    {
        int a, b;
        cin >> a >> b;
        s1[b] += a;
        s2[b] += a;
    }
    cin >> n;
    while(n -- )
    {
        int a, b;
        cin >> a >> b;
        s1[b] += a;
        s3[b] += a;
    }
    for (auto [x, v] : s2)
        for (auto [a, b] : s3)
            ans[x + a] += v * b;

    vector<PII> ans2;
    for (auto [x, v] : ans)
        if(v != 0)  ans2.pb({x, v});

    sort(ans2.begin(), ans2.end(), greater<PII>());
    int k2 = ans2.size();
    for (int i = 0; i < k2; i ++ )
        if(i)       cout << ' ' << ans2[i].y << ' ' << ans2[i].x;
        else cout << ans2[i].y << ' ' << ans2[i].x;
    if(ans2.size() == 0)    cout << 0 << " " << 0;
    cout << endl;

    vector<PII> ans1;
    for (auto [x, v] : s1)
        if(v != 0)  ans1.pb({x, v});
    sort(ans1.begin(), ans1.end(), greater<PII>());
    int k1 = ans1.size();
    for (int i = 0; i < k1; i ++ )
        if(i)       cout << ' ' << ans1[i].y << ' ' << ans1[i].x;
        else cout << ans1[i].y << ' ' << ans1[i].x;
    if(ans1.size() == 0)    cout << 0 << " " << 0;
    return 0;
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值