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; }
NBUT The Sum of F(x) and G(x),布布扣,bubuko.com
原文地址:http://blog.csdn.net/u011345136/article/details/24991649