题目
https://pintia.cn/problem-sets/994805046380707840/problems/994805060372905984
就是模拟多项式的除法
思路
找到多项式相除的普遍规律,然后模拟
一开始用的是结构体+vector,但是后来发现在AB系数不相等的时候,向前插入会出现错误
然后看到了有人用map做,觉得很不错,学习了一下
规律:A/B
- 先求出指数相减和系数相除,若A的指数<B,则跳出循环,计算结束,得到的A就是余数
- 否则,把结果赋给Q,然后A的第一项就可以消去了
- 遍历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的用法要熟练一些