多项式除法c++
L2-018 多項式A除以B (25分)
(最近重刷PTA天梯賽的題目碰到這個題,還是無從下手,所以寫篇博客記錄一下)
//輸入是以指數遞減的形式依次輸入每一項 4 4 1 2 -3 1 -1 0 -1 3 2 3 1 -2 0 1//輸出和輸入格式一樣,依次輸出商和余數 3 2 0.3 1 0.2 0 -1.0 1 1 -3.1樣例圖解
題目代碼
#include <bits/stdc++.h>using namespace std; typedef long long ll; #define lowbit(x) ((x) & (-x)) const int inf = 0x3f3f3f3f; const int N = 1010;double c1[N], c2[N], ans[N]; //ans數組保存商,c1數組最終的值就是余數 int max1, max2;void input(double *arr, int cnt, int &m) {int e;double val;while (cnt--) {cin >> e >> val;arr[e] = val;m = max(m, e);} }void output(double *arr, int e) {int cnt = 0;for (int i = 0; i <= e; i++) if (abs(arr[i]) >= 0.05) //四舍五入后為0.0的項不會輸出cnt++;if (cnt == 0) cout << "0 0 0.0";else {cout << cnt;for (int i = e; i >= 0; i--) {if (abs(arr[i]) >= 0.05) printf(" %d %.1f", i, arr[i]);}} }int main() { #ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);freopen("out.txt", "w", stdout); #endif int n;cin >> n, input(c1, n, max1);cin >> n, input(c2, n, max2);int d = max1 - max2; //保存商的最高項while (max1 >= max2) {double k = c1[max1] / c2[max2]; ans[max1 - max2] = k;for (int i = max1, j = max2; i >= 0 && j >= 0; i--, j--) c1[i] -= c2[j] * k;max1--;}output(ans, d);cout << endl;output(c1, max1);return 0; }總結
- 上一篇: cesium本地部署引入离线地图
- 下一篇: s3c2440移植MQTT