PAT甲級1002
這個問題講的就是多項式加起來,根據題意設置一個數組把係數存儲起來就好了,最後輸出。
Talk is cheap, show me the code.
#include <cstdio>
#define MAXK 1001
using namespace std;
double an[MAXK];
int main(void) {
// 多項式求和嘛,簡單
int cnt;
double a;
int ex;
for (int i = 0; i < MAXK; ++i) {
an[i] = 0;
}
scanf("%d", &cnt);
while (cnt--) {
scanf("%d %lf", &ex, &a);
an[ex] += a;
}
scanf("%d", &cnt);
while (cnt--) {
scanf("%d %lf", &ex, &a);
an[ex] += a;
}
cnt = 0;
for (int i = 0; i < MAXK; ++i) {
if (an[i] != 0) {
++cnt;
}
}
cnt == 0 ? printf("0\n") : printf("%d ", cnt);
for (int i = MAXK; i >= 0; --i) {
if (an[i] != 0) {
if (--cnt) {
printf("%d %.1f ", i, an[i]);
} else {
printf("%d %.1f\n", i, an[i]);
break;
}
}
}
return 0;
}
歡迎大家提出改進的建議。