L2-018 多项式A除以B

#include 
#include 
#include 
using namespace std;
typedef pair PII;
vector ans, rem;
void output(vector v) {
	for (vector::iterator it = v.begin(); it < v.end();) {
		if (abs(it->second) + 0.05 < 0.1) {
			it = v.erase(it);
		}
		else it++;
	}
	cout << v.size() << " ";
	if (v.empty()) {
		cout << "0 0.0" << endl;
		return;
	}
	int flag = 0;
	for (auto x : v) {
		if (flag) cout << " ";
		printf("%d %.1lf", x.first, x.second);
		flag = 1;
	}
	cout << endl;
	return;
}
void calc(vector v1, vector v2) {
	sort(v1.rbegin(), v1.rend());
	//sort(v2.rbegin(), v2.rend());
	int ind = v1[0].first - v2[0].first;
	if (ind < 0) {
		rem = v1;
		return;
	}
	double coe = v1[0].second / v2[0].second;
	ans.push_back(PII(ind, coe));
	vector v;
	for (int i = 1; i < v2.size(); i++) {
		int a = ind + v2[i].first;
		double b = coe * v2[i].second;
		v.push_back(PII(a, -b));
	}
	for (int i = 1; i < v1.size(); i++) {
		int flag = 1;
		for (int j = 0; j < v.size(); j++) {
			if (v1[i].first != v[j].first) continue;
			v[j].second += v1[i].second;
			flag = 0;
			break;
		}
		if (flag) v.push_back(PII(v1[i].first, v1[i].second));
	}
	calc(v, v2);
	return;
}
int main() {
	int n;
	double b;
	cin >> n;
	vector v1, v2;
	for (int i = 0, a; i < n; i++) {
		cin >> a >> b;
		v1.push_back(PII(a, b));
	}
	cin >> n;
	for (int i = 0, a; i < n; i++) {
		cin >> a >> b;
		v2.push_back(PII(a, b));
	}
	calc(v1, v2);
	output(ans);
	output(rem);
	return 0;
}

你可能感兴趣的:(算法题,算法,c++,数据结构)