PAT甲级 1002 A+B for Polynomials (25)(25 分)
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 分)的更多相关文章
- PAT 甲级 1002 A+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 ...
- PAT 甲级1002 A+B for Polynomials (25)
1002. A+B for Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue T ...
- PAT 甲级 1002 A+B for Polynomials
https://pintia.cn/problem-sets/994805342720868352/problems/994805526272000000 This time, you are sup ...
- PAT甲级——1002 A+B for Polynomials
PATA1002 A+B for Polynomials This time, you are supposed to find A+B where A and B are two polynomia ...
- pat 1002 A+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 ...
- PAT甲级:1066 Root of AVL Tree (25分)
PAT甲级:1066 Root of AVL Tree (25分) 题干 An AVL tree is a self-balancing binary search tree. In an AVL t ...
- PAT甲级:1124 Raffle for Weibo Followers (20分)
PAT甲级:1124 Raffle for Weibo Followers (20分) 题干 John got a full mark on PAT. He was so happy that he ...
- PAT甲级:1064 Complete Binary Search Tree (30分)
PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...
- 【PAT】1002. A+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 polynom ...
随机推荐
- RabbitMQ(4) 未路由的消息、TTL和死信
未路由的消息 当生产这发送的消息到达指定的交换器后,如果交换器无法根据自身类型.绑定的队列以及消息的路由键找到匹配的队列,默认情况下消息将被丢弃.可以通过两种方式 处理这种情况,一是在发送是设置man ...
- zabbix_agent中使用.pgpass
在配置zabbix过程中,使用了.pgpass 原理: psql -h 192.168.5.XXX -p 5433 -d mydb -U postgres 这个时候要输入密码:user_test 但是 ...
- 目标检测 - Tensorflow Object Detection API
一. 找到最好的工具 "工欲善其事,必先利其器",如果你想找一个深度学习框架来解决深度学习问题,TensorFlow 就是你的不二之选,究其原因,也不必过多解释,看过其优雅的代码架 ...
- EasyPusher/EasyDarwin支持H.265 RTSP/RTP直播推流与分发播放
前言描述 随着大屏时代和高清时代的到来,人们已经不再满足于VGA.CIF这种小分辨率了,取而代之的是720P.1080P.4K级的视频传输,虽然我们国家的基础带宽一直在上升,但普遍情况下,传输高清视频 ...
- python中继承和多态
继承和多态 继承 引入继承 我们有这样一个需求 模仿英雄联盟定义两个英雄类 1.英雄要有昵称.攻击力.生命值属性 2.实例化出两个英雄对象 3.英雄之间可以互殴,被殴打的一方掉血,血量小于0则判断为死 ...
- 最新博客开启 - Noogle's Blogs
博客地址: http://noogel.xyz/ 戳我进入 Noogle's Blogs
- Redis学习总结之三——Redis客户端命令
Windows命令行启动Redis客户端 1. 使用默认IP地址和端口连接Redis数据库(IP地址:127.0.0.1,端口号6379) redis-cli 2. 使用指定IP地址和端口连接Redi ...
- Loj 2534 异或序列
Loj 2534 异或序列 考虑莫队离线处理.每加一个数,直接询问 \(a[x]\oplus k\) 的前/后缀数目即可,减同理. 利用异或的优秀性质,可以维护异或前缀和,容易做到每次 \(O(1)\ ...
- BZOJ2588 SPOJ10628 Count on a tree 【主席树】
BZOJ2588 Count on a tree 题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中l ...
- python学习-序列排序
python的排序中,可以使用内置的sort()来对序列进行排序,也可以使用内置的sorted()函数对序列进行排序,区别是,当使用sort()时,是对原序列进行排序,而sorted()则是生成一个新 ...