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 ...
随机推荐
- New Concept English there (6)
30w/m The expensive shops in a famous arcade near Piccadilly were just opening. At this time of the ...
- 使用 create-react-app 构建 react应用程序
原文 http://blog.csdn.net/github_squad/article/details/57452333#
- css hover伪类选择器与JQuery hover()方法
css hover伪类选择器 它属于anchor伪类 在支持 CSS 的浏览器中,<a>标签链接的不同状态都可以以不同的方式显示,常常用来改链接的颜色效果 实例 a:link {color ...
- ubuntu16扩展屏设置
new ubuntu system setting - Expansion screen settings. 1,System Settings–>Displays 1,set big scre ...
- EasyPlayer Android基于ffmpeg实现播放(RTSP/RTMP/HTTP/HLS)同步录像功能
之前有博客专门介绍了EasyPlayer的本地录像的功能,简单来说,EasyPlayer是一款RTSP播放器,它将RTSP流里的音视频媒体帧解析出来,并用安卓系统提供的MediaMuxer类进行录像. ...
- Python如何输出带颜色的文字
print('\033[31m%s' % "这是前景色") 这是前景色 print('%s' % "这是前景色") 这是前景色 print('\033[1;31 ...
- Linux usleep for shell
/**************************************************************************** * Linux usleep for she ...
- Linux shell multifile content replace with sed
#!/bin/bash # Linux shell multifile content replace with sed # 声明: # 本源代码主要是利用两份(中.英文)具有相同键值对的json数据 ...
- vue样式穿透
在一次这样的需求中,需要实现滑倒底部时自动请求数据,需要动态创建节点然后追加到某元素中,这期间遇到的问题就是在动态创建节点后,类名也已经加上了 ,但是样式就是没有生效,最后发现原因的产生竟然是< ...
- Redis学习总结之一——Redis初入
Redis简介 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API. Redis是一个key-value存储系统. ...