【Openjudge】多项式加法

来源:互联网 发布:csgo国服mac 编辑:程序博客网 时间:2024/06/06 16:31

数组模拟链表,coefficient为0的项并没有被删除,只是输出时忽略了。

#include<iostream>#include<cstring>using namespace std;int main(){int n;int a[505][3] = {};cin >> n;int e, p;int tail, listtail;int idx, pidx;while(n --){memset(a, -1, sizeof(a));tail = 1;for (int i = 0; i < 2; i ++){while (cin >> e >> p){if (p < 0)break;idx = 0;pidx = 0;while (idx != -1 && (idx == 0 || p < a[idx][2])){pidx = idx;idx = a[idx][0];}if (idx != -1 && p == a[idx][2])a[idx][1] += e;else{a[tail][0] = idx;a[tail][1] = e;a[tail][2] = p;a[pidx][0] = tail;tail ++;}}}for (idx = a[0][0]; idx != -1; idx = a[idx][0]){if (a[idx][1] != 0)printf("[ %d %d ] ", a[idx][1], a[idx][2]);}cout << endl;}return 0;}


原创粉丝点击