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. java注解及在butternife中的实践和原理

    1.  背景 之前去一个公司,说到了java的注解,问java的注解有几种方式,然后我提到了android中的butternife和afinal注解工具,我们知道butternife在6.1版本的时候 ...

  2. 那些年Android开发中遇到的坑

    使用静态变量来缓存数据时,不管是在Application类还是其他类,都要注意因应用重建而引发的问题. 使用DecorView作为PopupWindow的anchorView时,在华为P7中它是显示在 ...

  3. objective-c中所谓的僵尸对象

    正常情况下向已回收的对象发送消息时灵时不灵,具体要看该对象所占内存有没有被覆写.cocoa提供了僵尸对象(Zombie Object)这个功能,简单的说:启用该调试功能后,运行时会将所有已回收的实例转 ...

  4. Oracle与Mysql时间格式化

    一,Oracle格式化时间: Oracle 获取当前日期及日期格式 获取系统日期:  SYSDATE()    格式化日期:     TO_CHAR(SYSDATE(),'YY/MM/DD HH24: ...

  5. Linux下安装MQ

    1.下载Linux下MQ的安装包,网上下载试用版或购买正版,此处以7.0.0.0版为例安装 2.如上图所示,是linux的MQ安装包展开图 3.创建用户和用户组 >root用户连接linux & ...

  6. Spring Boot 多模块与 Maven 私有仓库

    前言 系统复杂了,抽离单一职责的模块几乎是必须的:若需维护多个项目,抽离公用包上传私有仓库管理也几乎是必须的.其优点无需赘述,以下将记录操作过程. 1. 多模块拆分 在.NET 中由于其统一性,实现上 ...

  7. IE中调试JS的一款很好的工具

    附件是 IE中调试JS的一款很好用的工具,欢迎下载使用.  具体使用方法为:  1.先安装Companion.JS文件(install.exe).  2.安装Microsoft Script Debu ...

  8. 关于Linux和Unix的分析

    Linux操作系统即linux.  Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和UNIX的多用户.多任务.支持多线程和多CPU的操作系统.它能运行主要的UNIX工具软 ...

  9. Django1.8文档阅读手记

    主要集中在新特性上. Django1.8好像开始推荐使用python3 Django的QuerySet是延迟加载的,这个文档里面一般会明言,通过Connection SQL显示测试,外键对象也是延迟加 ...

  10. XYC2016上半年工作笔记整理

    只要团队在,做那个方向都可能 这个产品的用户群人均价值高 第一次产品介绍会议就介绍了产品的初期全部目标功能 传统互联网人的产品思路比较偏媒体内容服务特性. 产品转化率高说明了其发展势头 任何一个形式变 ...