#2017# 10-多项式A除以B

题目

https://pintia.cn/problem-sets/994805046380707840/problems/994805060372905984
就是模拟多项式的除法

思路

找到多项式相除的普遍规律,然后模拟
一开始用的是结构体+vector,但是后来发现在AB系数不相等的时候,向前插入会出现错误
然后看到了有人用map做,觉得很不错,学习了一下
规律:A/B

  1. 先求出指数相减和系数相除,若A的指数<B,则跳出循环,计算结束,得到的A就是余数
  2. 否则,把结果赋给Q,然后A的第一项就可以消去了
  3. 遍历AQ,把不符合条件的项删去,最后输出

代码

#include <bits/stdc++.h>

#define maxx 5010
typedef long long ll;
using namespace std;
map<int, double, greater<int>> A, B, Q, QQ, R;

void add()
{
    while (A.begin()->first >= B.begin()->first)//一开始没排序,结果为0
    {
        int n = A.begin()->first - B.begin()->first;
        double a = A.begin()->second / B.begin()->second;
        Q[n] += a;
        for (auto &it:B)
        {
            A[it.first + n] -= it.second * a;
        }
        A.erase(A.begin());
    }
    //map--"erase"
    for (auto &it:A)
    {
        if (abs(it.second) >= 0.05)
            R[it.first] = it.second;
    }
    for (auto it:Q)
    {
        if (abs(it.second) >= 0.05)
            QQ[it.first] = it.second;
    }
    cout << QQ.size();
    if (!QQ.empty())
    {
        for (auto item:QQ)
        {
            printf(" %d %.1f", item.first, item.second);
        }
    }
    else
        cout << " 0 0.0";
    cout << endl;
    cout << R.size();
    if (!R.empty())
    {
        for (auto item:R)
        {
            printf(" %d %.1f", item.first, item.second);
        }
    }
    else
        cout << " 0 0.0";
}

int main()
{
    int N;
    cin >> N;
    while (N--)
    {
        int x, y;
        cin >> x >> y;
        A[x] = y;
    }
    cin >> N;
    while (N--)
    {
        int x, y;
        cin >> x >> y;
        B[x] = y;
    }
    add();
    return 0;
}


总结

map的用法要熟练一些

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值