PAT 1002. A+B for Polynomials (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 aN1 N2 aN2 ... NK aNK, where K is the number of nonzero terms in the polynomial, Ni and aNi (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
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner; public class Main {
public static void main(String[] args) {
Queue<A> q1 = new LinkedList<A>();
Queue<A> q2 = new LinkedList<A>();
Queue<A> q3 = new LinkedList<A>();
Scanner input = new Scanner(System.in);
int n = input.nextInt();
for(int i=0;i<n;i++){
A a = new A();
a.a = input.nextInt();
a.b = input.nextDouble();
q1.add(a);
}
int m = input.nextInt();
for(int i=0;i<m;i++){
A a = new A();
a.a = input.nextInt();
a.b = input.nextDouble();
q2.add(a);
}
while(!q1.isEmpty()&&!q2.isEmpty()){
int a1 = q1.peek().a;
double b1 = q1.peek().b;
int a2 = q2.peek().a;
double b2 = q2.peek().b;
A a = new A();
if(a1==a2){
a.a = a1;
a.b = b1+b2;
q1.poll();
q2.poll();
}if(a1>a2){
a.a = a1;
a.b = b1;
q1.poll();
}if(a1<a2){
a.a = a2;
a.b = b2;
q2.poll();
}
if(a.b!=0)
q3.add(a);
}
while(!q1.isEmpty()){
q3.add(q1.poll());
}
while(!q2.isEmpty()){
q3.add(q2.poll());
}
System.out.print(q3.size()); while(!q3.isEmpty()){
System.out.print(" "+q3.peek().a+" ");
System.out.printf("%.1f",q3.peek().b);
q3.poll();
} }
}
class A{
int a;
double b;
}
PAT 1002. A+B for Polynomials (25)的更多相关文章
- 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 (25分)
题目 This time, you are supposed to find A+B where A and B are two polynomials. Input Specification: E ...
- PAT (Advanced Level) Practice 1002 A+B for Polynomials (25 分) 凌宸1642
PAT (Advanced Level) Practice 1002 A+B for Polynomials (25 分) 凌宸1642 题目描述: This time, you are suppos ...
- 【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 ...
- 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 po ...
- 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 (25) 2016-09-09 22:50 64人阅读 评论(0) 收藏
1002. A+B for Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue T ...
- 1002 A+B for Polynomials (25)(25 point(s))
problem 1002 A+B for Polynomials (25)(25 point(s)) This time, you are supposed to find A+B where A a ...
- 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 ...
随机推荐
- WPF入门教程系列四
WPF之Binding的使用(二) 一. 前言 初学WPF经常被Binding搞得苦不堪言,Binding的重用性就不做介绍了,在WPF应用程序开发中Binding是一个非常重要的部分.WPF也是近 ...
- DNS的概念,用途,DNS查询的实现算法
1.DNS的概念,用途 DNS是由解析器以及域名服务器组成的. 域名服务器是指保存有该网络中所有主机的域名和对应IP地址,并具有将域名转换为IP地址功能的服务器. DNS ...
- 一个关于git push失败的解决方案
问题背景:在GitHub上创建了一个repositorie, 本地初始化并添加了远程仓库后,在GitHub上创建了一个README.md文件(注意不是从本地git push上去的),随后本地修改工程源 ...
- Java 8中的 Streams API 详解
为什么需要 Stream Stream 作为 Java 8 的一大亮点,它与 java.io 包里的 InputStream 和 OutputStream 是完全不同的概念.它也不同于 StAX 对 ...
- Android WebView与JavaScript交互实现Web App
当我们去开发一个基于web的android app时,我们第一须要处理的就是与JavaScript的交互问题.Android须要做的事情就是开放某些特定的接口供web里的JavaScript调用,能够 ...
- C++语言基础(2)-new和delete操作符
在C语言中,动态分配内存用 malloc() 函数,释放内存用 free() 函数.如下所示: ); //分配10个int型的内存空间 free(p); //释放内存 在C++中,这两个函数仍然可以使 ...
- JME的flyCam和cam的区别
http://hub.jmonkeyengine.org/wiki/doku.php/jme3:advanced:camera 注意这句话: The flyCam class field gives ...
- hdu1695 GCD2 容斥原理 求x属于[1,b]与y属于[1,d],gcd(x,y)=k的对数。(5,7)与(7,5)看作同一对。
GCD Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s): Accepted Sub ...
- ThinkPHP 处理商品添加的时候操作多张表 用事务解决。
#重新父类的add方法 public function add(){ #同时操作多装表,可以考虑用事务来做,要同时插入数据成功要么都不插输入数据. #开启事务的前提是表的引擎必须是InnoDB #开启 ...
- Easyui Datagrid扩展fixRownumber方法
首先,从datagrid生成的代码,我们可以发现,在rowNumber上都有特定的class标记,datagrid-cell-rownumber,datagrid-header-rownumber. ...