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 ...
随机推荐
- php中的可变变量、可变函数、匿名函数
1.可变变量 简而言之:获取一个普通变量的值作为这个可变变量的变量名. 如: $a = "hello"; $$a = " world"; /* $a 的值为&q ...
- opencv中各种矩阵乘的差别
尊重原创,转载请注明:http://blog.csdn.net/tangwei2014 OpenCV中每次遇到矩阵乘法就乱,各种翻各种查. 这次总结了一下.为了简单明了,还是让样例说话. 1. Mat ...
- java 中 HashMap 遍历与删除
HashMap的遍历 方法一.这是最常见的并且在大多数情况下也是最可取的遍历方式 /** * 在键值都需要时使用 */ Map<Integer, Integer> map = new Ha ...
- Python内置函数之isinstance()
isinstance(object,classinfo)用来判断对象是否为某种数据类型. 例子: >>> isinstance(,object) True >>> ...
- linux fedora 14(内核2.6.35.6) PF_RING+libpcap 极速捕获千兆网数据包,不丢包
前面讲到了libpcap 捕获数据包,尤其在千兆网的条件下,大量的丢包,网上搜索好久,大概都是PF_PACKET +MMAP,NAPI,PF_RING之类的方法,我对PF_RING+libpcap进行 ...
- driver基础
驱动测试时,linux驱动常以模块方式插入内核.应包含头文件:linux/kernel.h,linux/module.h 设备驱动的一般结构:Soc(主芯片->设备控制器->外设引脚)-- ...
- 解决PHP编译cURL的reinstall the libcurl问题
今天正好要用到PHP的curl功能,发现服务器上的PHP并没有配置curl,进而查询PHP官方文档,得知编译PHP时需要带上 –with-curl参数,才能把curl模块编译进去.我现在PHP已经编译 ...
- X264参考手册
艺搜简介 基本语法: x264 [options]-o outfile infile 注意与ffmpeg的输入输出文件位置恰好相反: ffmpeg[options][[infile options]- ...
- 第一百七十节,jQuery,事件对象,event 对象,默认行为,冒泡
jQuery,事件对象,event 对象,默认行为,冒泡 学习要点: 1.事件对象 2.冒泡和默认行为 JavaScript 在事件处理函数中默认传递了 event 对象,也就是事件对象.但由于浏览器 ...
- JNDI提供了一种统一的方式,可以用在网络上查找和访问服务
JNDI提供了一种统一的方式,可以用在网络上查找和访问服务.通过指定一个资源名称,该名称对应于数据库或命名服务中的一个记录,同时返回数据库连接建立所必须的信息. JNDI主要有两部分组成:应用程序编程 ...