1002 A+B for Polynomials (25)(25 分)

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 a~N1~ N2 a~N2~ ... NK a~NK~, where K is the number of nonzero terms in the polynomial, Ni and a~Ni~ (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

1002.多项式A与B的和

这次,假设A和B是两个多项式,求A与B的和多项式。

输入

每个输入文件包含一个测试实例。每个实例有两行,每行包含一个多项式的信息: K N1 aN1 N2 aN2 ... NK aNK,其中K为多项式中非0项的个数,Ni 和 aNi (i=1, 2, ..., K) 分别为指数和系数。数的范围是1 <= K <= 10,0<= NK < ... < N2 < N1 <=1000。

输出

对于每个测试实例,你需要在一行内输出A与B的和,格式与输入时相同。注意每行的结尾不能有多余的空格。小数精确到一位。


多项式求和
可能会有负数
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
double a[];
bool b[];
struct node
{
int x;
double y;
};
int main()
{
int n,m;
int max=;
while(cin>>n)
{
memset(a,,sizeof(a));
memset(b,,sizeof(b));
int s=;
max=;
for(int i=;i<=n;i++)
{
int x;
double y;
cin>>x>>y;
a[x]+=y;
if(x>max) max=x;
if(!b[x])
{
b[x]=;
}
}
cin>>m;
for(int i=;i<=m;i++)
{
int x;
double y;
cin>>x>>y;
a[x]+=y;
if(x>max) max=x;
if(!b[x])
{
b[x]=;
}
}
queue<node>q;
while(!q.empty ()) q.pop();
for(int i=max;i>=;i--)
{
if(a[i]!=)
{
node p;
p.x=i;
p.y=a[i];
q.push (p);
s++;
}
}
cout<<s;
while(!q.empty ())
{
node p=q.front();
q.pop();
printf(" %d %.1lf",p.x,p.y);
}
cout<<endl; }
return ; }
 

最新文章

  1. Linux常用命令学习6---(vim的使用)
  2. 【运维工具】Git代码发布系统
  3. 通过全局getApp获取全局实例获取数据
  4. 升级OS X EI Capition 版本导致cocoapods 使用终端上pod: command not found
  5. iOS之UI--UITabBarController
  6. PHP笔试题(转载)
  7. Spark基础与Java Api介绍
  8. SQL之50个常用的SQL语句
  9. ubuntu上如何安装和卸载google chrome 浏览器
  10. .net开发windows服务
  11. 【剑指offer】面试题42:翻转单词顺序 VS 左旋转字符串
  12. Andy的生活
  13. Spring 设置readonly 事务只读模式
  14. Swift3.0 创建工程常用的类、三方、以及扩展 1.5
  15. 如何实现VM框架中的数据绑定
  16. windows&amp;lunix下node.js实现模板化生成word文件
  17. BootStrap分页教程
  18. windows下配置maven
  19. ELK5.3日志分析平台&amp;部署
  20. myeclipse 怎么安装与激活

热门文章

  1. jQuery动画与特效
  2. Redis数据结构:SDS
  3. 《Effective C++》第2章 构造/析构/赋值运算(1)-读书笔记
  4. New Concept English three(20)
  5. 基于EasyDSS流媒体服务器实现的直播流管理与鉴权的后台方案
  6. python中对文件的处理
  7. js 以函数名作为参数动态执行 函数
  8. angular 与jQuery混用 大坑一
  9. 物体识别重要指标——平均准确率(Average Precision, AP )
  10. [剑指offer]数组中最小的K个数,C++实现