- 问题描述
-
When Deathmoon played MC game, he faced a math problem. When he found a ancient tomb and came in, he found two polynomials f(x) and g(x) no the wall, only did he calculate f(x) + g(x) correctly he could come in, can you help him?
For example:
f(x) = 2*x^5 + 3*x^3 + 7 + x^(-1),
g(x) = 3*x^4 + 2*x^3 + x - x^(-1).
Then,
f(x) + g(x) = 2*x^5 + 3*x^4 + 5*x^3 + x + 7
- 输入
-
Input end of EOF.
First I will give you two positive integers N and M(0 < N, M < 10) means the number of items in polynomials.
Then N lines follow, each line contains two integers c(-100 < c < 100) and p(-10 <= p <= 10), c means the coefficients and p means the power.
Then M lines follow, each line contains two integers c(-100 < c < 100) and p(-10 <= p <= 10), c means the coefficients and p means the power.
And for each test, every polynomial' s power is descending, it means pi > pj when i < j.
c != 0 && p != 0.
- 输出
-
You should output (f(x) + g(x))' s expression for coefficients and powers in descending.
- 样例输入
-
4 4 2 5 3 3 7 0 1 -1 3 4 2 3 1 1 -1 -1
- 样例输出
-
2 5 3 4 5 3 1 1 7 0
- 提示
-
f(x) = coefficients * x ^ power; The sample input: f(x) = 2*x^5 + 3*x^3 + 7 + x^(-1), g(x) = 3*x^4 + 2*x^3 + x - x^(-1). The sample output: f(x) + g(x) = 2*x^5 + 3*x^4 + 5*x^3 + x + 7
题意:显而易见
思路:纯模拟,让下标确定为正数就是了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAXN = 1000;
int n,m;
int arr[MAXN];
int vis[MAXN];
int main(){
while (scanf("%d%d", &n, &m) != EOF){
int c,p;
memset(vis,0,sizeof(vis));
for (int i = 0; i < n; i++){
scanf("%d%d", &c, &p);
arr[p+10] = c;
vis[p+10] = 1; //确保正数
}
for (int i = 0; i < m; i++){
scanf("%d%d", &c, &p);
if (vis[p+10]){
arr[p+10] += c;
if (arr[p+10] == 0)
vis[p+10] = 0;
}
else {
vis[p+10] = 1;
arr[p+10] = c;
}
}
for (int i = 20; i >= 0; i--)
if (vis[i])
printf("%d %d\n", arr[i], i-10);
}
return 0;
}