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 ...
随机推荐
- gridview 后台增加列
BoundField field1 = null; field1 = new BoundField(); //实例化 field1.HeaderText = "序号";field ...
- LeetCode OJ:Convert Sorted List to Binary Search Tree(将排序好的链表转换成二叉搜索树)
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- procrdure存储过程
/* 存储过程 在一些语言中,有一个概念叫”过程“ procedure,和”函数“ function 过程:封装了若干条语句,调用时,这些封装体执行 函数:是一个有返回值的“过程” 过程:没有返回值的 ...
- C#调用C++编译的DLL--
引用 C#调用C++编译的DLL详解 可以单步跟踪进入dll的源码!每种编程语言调用DLL的方法都不尽相同,在此只对用C#调用DLL的方法进行介绍.首先,您需要了解什么是托管,什么是非托管.一般可以认 ...
- 从头搭建一个React应用
node,webpack这些就不一一介绍怎么安装了,默认大家都知道. 一.npm install -g create-react-app 首先全局安装react的脚手架工具 create-react- ...
- Java基础面试题 (一)
1.面向对象的三个特征 封装,继承,多态.这个应该是人人皆知,有时候也会加上抽象. 2.多态的好处 允许不同类对象对同一消息做出响应,即同一消息可以根据发送对象的不同而采用多种不同的行为方式(发送消息 ...
- 程序设计入门-C语言基础知识-翁恺-第六周:数组-详细笔记(六)
目录 第六章:数组 6-1 数组 6-2 数组计算 6.3 课后习题 第六章:数组 6-1 数组 题目:让用户输入一组整数以-1结束输入,算出这组数的平均值,并且输出大于平均值的数. 我们需要记录用户 ...
- 数据库备份mysqldump
|--好友说:有些知识就是石头,走上这条路就没法绕过,就是牙齿啃碎了也要把它啃下来. ------------------------------------------ 针对当前备份做一个总结记忆: ...
- HDU - 4339: Query(bitset暴力找下一个为1的)
题意:给定A,B长度相同的字符串,Q次操作,修改操作位单个字符修改,查询操作为询问从某点开始有多少连续相同的字符. 思路:我们把不相同的设为1,相同的设为0,那么询问就是找下一个为1的为位置,可以用线 ...
- python发送邮件的实例代码(支持html、图片、附件)
转自http://www.jb51.net/article/34498.htm 第一段代码 #!/usr/bin/python# -*- coding: utf-8 -*- import emaili ...