PAT甲级 1002 A+B for Polynomials (25)(25 分)

时间:2022-09-03 09:05:00
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 ; }
 

PAT甲级 1002 A+B for Polynomials (25)(25 分)的更多相关文章

  1. PAT 甲级 1002 A&plus;B for Polynomials (25 分)

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

  2. PAT 甲级1002 A&plus;B for Polynomials &lpar;25&rpar;

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

  3. PAT 甲级 1002 A&plus;B for Polynomials

    https://pintia.cn/problem-sets/994805342720868352/problems/994805526272000000 This time, you are sup ...

  4. PAT甲级——1002 A&plus;B for Polynomials

    PATA1002 A+B for Polynomials This time, you are supposed to find A+B where A and B are two polynomia ...

  5. pat 1002 A&plus;B for Polynomials (25 分)

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

  6. PAT甲级:1066 Root of AVL Tree &lpar;25分&rpar;

    PAT甲级:1066 Root of AVL Tree (25分) 题干 An AVL tree is a self-balancing binary search tree. In an AVL t ...

  7. PAT甲级:1124 Raffle for Weibo Followers &lpar;20分&rpar;

    PAT甲级:1124 Raffle for Weibo Followers (20分) 题干 John got a full mark on PAT. He was so happy that he ...

  8. PAT甲级:1064 Complete Binary Search Tree &lpar;30分&rpar;

    PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...

  9. 【PAT】1002&period; A&plus;B for Polynomials &lpar;25&rpar;

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

随机推荐

  1. iOS 内存管理

    一 . 内存管理 包括内存分配 和 内存清除 1.内存管理的范围 :人和继承于NSObject类的对象都需要进行内存管理,任何非对象类型的对象(基本数据类型 如 int char float doub ...

  2. 【MYSQL】数据类型

    转载 https://www.baidu.com/s?ie=UTF-8&wd=cnblog 原文 泪云山海的博客 mysql 数据类型 1.整型 MySQL数据类型 含义(有符号) tinyi ...

  3. Cocos2d-x优化中多线程并发访问

    多线程并发访问在Cocos2d-x引擎中用的不是很多,这主要是因为中整个结构设计没有采用多线程.源自于Objective-C的Ref对象,需要使用AutoreleasePool进行内存管理,Autor ...

  4. 在swift中使用MJRefresh

    cocoapod导入的,并且桥接已经完成,但是就是不提示方法,醉了,

  5. threadid&equals;1&colon; thread exiting with uncaught exception &lpar;group&equals;0x40db8930&rpar;

    异常信息如下: 07-26 17:23:49.521: W/dalvikvm(29229): threadid=1: thread exiting with uncaught exception (g ...

  6. XSS学习笔记(五)-XSS防御

    如果只生产XSS的地方都与输入或输出相关联的.所以错过了主要矛盾.而且,我们将有一个解决问题的办法:您可以输入端砚格过滤,是可能的过滤输出时间,输出到用户的GET或POST中是否有敏感字符: 输入过滤 ...

  7. 修改Linux内核参数提高Nginx服务器并发性能

    当linux下Nginx达到并发数很高,TCP TIME_WAIT套接字数量经常达到两.三万,这样服务器很容易被拖死.事实上,我们可以简单的通过修改Linux内核参数,可以减少Nginx服务器 的TI ...

  8. 使用nginx代理kibana并配置登录验证

    由于kibana不支持登录验证,谁都可以访问,放到公网就不合适了,这里配置用nginx进行代理: 生成密码文件 如果安装了httpd可以用htpasswd,比较方便: htpasswd -c /roo ...

  9. AE10&period;0及AE10&period;0以上的版本调用ESRI&period;ArcGIS&period;esriSystem出现的问题

    如果本地安装的是AE10.0以上,那么添加ESRI.ArcGIS.esriSystem引用时,会出现esriLicenseProductCode并不包含esriLicenseProductCodeAr ...

  10. 生命周期函数以及vue的全局注册

    beforeCreate 在创造实例之前 created 创造实例以后 beforeMount 在挂载前 render 渲染节点到页面上 //将虚拟dom数组渲染出来 mounted 挂载以后 bef ...