首页 > 学院 > 开发设计 > 正文

PAT (Advanced Level) Practise 1002. A+B for Polynomials (25)

2019-11-06 09:22:44
字体:
来源:转载
供稿:网友

1002. A+B for Polynomials (25) 时间限制: 400 ms内存限制: 65536 kB代码长度限制: 16000 B判题程序:Standard
  This time, you are supposed to find A+B where A and B are two polynomials. Input   Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N1 aN1 N2 aN2 … NK aNK, where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1, 2, …, K) are the exponents and coefficients, respectively. It is given that 1 <= K <= 10,0 <= NK < … < N2 < N1 <=1000. Output   For each test case you should output the sum of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate to 1 decimal place. Examples

Input
2 1 2.4 0 3.22 2 1.5 1 0.5
Output
3 2 1.5 1 2.9 0 3.2

  Notes    作者   CHEN, Yue

  模拟多项式加法,注意系数为零的项不计也不显示。

#include <iostream>#include <algorithm>#include <map>#include <vector>#include <functional>#include <string>#include <cstring>#include <queue>#include <set>#include <stack>#include <cmath>#include <cstdio>#include <sstream>#include <iomanip>using namespace std;#define IOS ios_base::sync_with_stdio(false)#define TIE std::cin.tie(0)#define MIN2(a,b) (a<b?a:b)#define MIN3(a,b) (a<b?(a<c?a:c):(b<c?b:c))#define MAX2(a,b) (a>b?a:b)#define MAX3(a,b,c) (a>b?(a>c?a:c):(b>c?b:c))typedef long long LL;typedef unsigned long long ULL;const int INF = 0x3f3f3f3f;const double PI = 4.0*atan(1.0);const double eps = 1e-6;int a, b, n;double c;int main(){ n = 0; map<int, double> m; for (int i = 0; i < 2; i++){ scanf("%d", &a); for (int j = 0; j < a; j++){ scanf("%d%lf", &b, &c); if (m.count(b)) m[b] += c; else m[b] = c; } } map<int, double>::reverse_iterator it; for (it = m.rbegin(); it != m.rend(); it++){ if (fabs(it->second) > eps) n++; } PRintf("%d", n); for (it = m.rbegin(); it != m.rend(); it++){ if (fabs(it->second) > eps) printf(" %d %.1lf", it->first, it->second); } printf("/n"); //system("pause");}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表