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 ...
随机推荐
- LeetCode OJ:Isomorphic Strings(同构字符串)
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- LeetCode OJ:Palindrome Partitioning(回文排列)
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- LeetCode OJ:Decode Ways(解码方法)
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- gradle 删除指定目录中的文件和目录
// 删除bakAPk下的所有非母包文件 task deleTask(type: Delete){ FileTree tree = fileTree(dir: bakPath) tree.each { ...
- Python中列表生成式和字典生成式练习
(一)列表生成式 练习一:编写名为collatz(number)的函数:实现的功能:参数为偶数时,打印number// 2;参数为奇数时,打印3*number + 1 解析: number = int ...
- IIS站点/虚拟目录中访问共享目录(UNC)以及建立后的应用程序的信任级别问题
UNC是 Universal Naming Convention 的简称,也叫通用命名规范.通用命名约定.网络(范指局域网)上资源的完整位置名称.格式为 \\servername\sharenam ...
- PostgreSQL CheckPoint设置(转)
今天在研究checkpoint process的问题时,顺便复习了一下checkpoint设置问题,又有新的疑惑了. checkpoint又名检查点,在oracle中checkpoint的发生意味着之 ...
- Linux:xargs命令详解
xargs 给其他命令传递参数的一个过滤器,也是组合多个命令的一个工具.它擅长将标准输入数据转换成命令行参数,xargs能够处理管道或者stdin并将其转换成特定命令的命令参数.xargs也可以将单行 ...
- 虚拟机中centos7与物理主机通讯
本地物理机 WIN命令行:ipconfig 查看网络配置 在物理机的网络配置--> 配置VMnet8 打开VMware 编辑虚拟机设置,选择自定义NAT模式(VMnat8) 编辑->虚拟 ...
- Android编程 高德地图 AMapLocationClientOption 类中 setWifiActiveScan过时
高德地图中 定位包中有以下方法: AMapLocationClientOption 类中 setWifiActiveScan 过时 isWifiActiveScan public boole ...