1002. A+B for Polynomials (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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.

Sample Input

2 1 2.4 0 3.2
2 2 1.5 1 0.5

Sample Output

3 2 1.5 1 2.9 0 3.2

思路

题目要求打印两个多项式相加后的多项式的指数和系数。

用map<指数,系数>这样的关联形式来模拟相加就很简单了,找到对应的指数(键)计算对应的系数(值)就行。
特别注意的情况:指数的系数相加如果为0,那么这个指数和其对应的系数就不用输出了(相消了),而且对应的第一个输出的数字——指数个数也要减1。 代码
#include<iostream>
#include<map>
#include<iomanip>
#include<iterator>
using namespace std;
int main()
{
int k1,ksum = ;
while(cin >> k1)
{
map<int,double> sum; //<指数,系数>
for(int i = ;i < k1;i++)
{
int n;double a;
cin >> n >> a;
sum.insert(pair<int,double>(n,a));
ksum++;
} int k2;
cin >> k2;
for(int i = ;i < k2;i++)
{
int n;double a;
cin >> n >> a;
if(sum.count(n) > )
{
sum[n] += a;
if(sum[n] == )
ksum--;
}
else
{
sum.insert(pair<int,double>(n,a));
ksum++;
} } cout << ksum;
for(map<int,double>::reverse_iterator it = sum.rbegin(); it != sum.rend();it++)
{
if(it->second != )
{
cout << " " << it->first;
cout <<" "<< fixed << setprecision() << it->second;
} }
cout << endl;
}
}

PAT1002:A+B for Polynomials的更多相关文章

  1. pat1002. A+B for Polynomials (25)

    1002. A+B for Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue T ...

  2. 1002. A+B for Polynomials (25)

    题目链接:https://www.patest.cn/contests/pat-a-practise/1002 原题如下: This time, you are supposed to find A+ ...

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

    [题目链接] This time, you are supposed to find A+B where A and B are two polynomials. Input Each input f ...

  4. \(\S1 \) Gaussian Measure and Hermite Polynomials

    Define on \(\mathbb{R}^d\) the normalized Gaussian measure\[ d \gamma(x)=\frac{1}{(2\pi)^{\frac{d}{2 ...

  5. 1002. A+B for Polynomials

    1002. A+B for Polynomials (25) This time, you are supposed to find A+B where A and B are two polynom ...

  6. Legendre polynomials

    In mathematics, Legendre functions are solutions to Legendre's differential equation: In particular, ...

  7. PAT 解题报告 1009. Product of Polynomials (25)

    This time, you are supposed to find A*B where A and B are two polynomials. Input Specification: Each ...

  8. 【PAT】1009. Product of Polynomials (25)

    题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1009 分析:简单题.相乘时指数相加,系数相乘即可,输出时按指数从高到低的顺序.注意点:多项式相 ...

  9. PAT1002

    This time, you are supposed to find A+B where A and B are two polynomials. 这一次,你被要求计算A+B当A和B是两个多项式的时 ...

随机推荐

  1. python模块 - 常用模块推荐

    http://blog.csdn.net/pipisorry/article/details/47185795 python常用模块 压缩字符 当谈起压缩时我们通常想到文件,比如ZIP结构.在Pyth ...

  2. mysql进阶(九)多表查询

    MySQL多表查询 一 使用SELECT子句进行多表查询 SELECT 字段名 FROM 表1,表2 - WHERE 表1.字段 = 表2.字段 AND 其它查询条件 SELECT a.id,a.na ...

  3. DrawerLayout实现网易新闻抽屉效果

    个人感觉网易的客户端比较前卫,有很多新鲜的东西,有时候模仿这些好的客户端能学到很多东西 开始今天的主要课题,下面是网易客户端抽屉模式实现的效果 其实有个Drawerlayout这个布局,你得问题就已经 ...

  4. "《算法导论》之‘线性表’":双向循环链表

    本文双链表介绍部分参考自博文数组.单链表和双链表介绍 以及 双向链表的C/C++/Java实现. 1 双链表介绍 双向链表(双链表)是链表的一种.和单链表一样,双链表也是由节点组成,它的每个数据结点中 ...

  5. Android群英传笔记——摘要,概述,新的出发点,温故而知新,可以为师矣!

    Android群英传笔记--摘要,概述,新的出发点,温故而知新,可以为师矣! 当工作的越久,就越感到力不从心了,基础和理解才是最重要的,所以买了两本书,医生的<Android群英传>和主席 ...

  6. BI过程简述

    BI流程: 需求分析->维度设计->查询service->ETL倒数据->CDC监听数据库 需求分析:首先确定好的数据来源(多个数据库+excel文件+日志+...),需要的数 ...

  7. LeetCode(62)-Two Sum

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  8. mysql性能优化之-innodb_flush_log_at_trx_commit

    innodb_flush_log_at_trx_commit是配置MySql日志何时写入硬盘的参数: 一.参数值说明 0:log buffer将每秒一次地写入log file中,并且log file的 ...

  9. Opencv3.1+python2.7的CentOS7安装

    http://blog.csdn.NET/daunxx/article/details/50506625 转载注释:有些包名和系统包名并不匹配,yum install 找不到的时候,可以yum sea ...

  10. .net core使用orm操作mysql数据库

    Mysql数据库由于其体积小.速度快.总体拥有成本低,尤其是开放源码这一特点,许多中小型网站为了降低网站总体拥有成本而选择了MySQL作为网站数据库.MySQL是一个多用户.多线程的关系型数据库管理系 ...