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 ...
随机推荐
- jQuery框架源码解读
1.jQuery 1.9.1 parseJSON: function( data ) { // Attempt to parse using the native JSON parser first ...
- 哈希—— POJ 3349 Snowflake Snow Snowflakes
相应POJ题目:点击打开链接 Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions ...
- webAPP开发的问题(总结)
自Iphone和Android这两个牛逼的手机操作系统公布以来,在互联网界从此就多了一个新的名词-WebApp(意为基于WEB形式的应用程序.执行在高端的移动终端设备). 开发人员们都知道在高端智能手 ...
- [ci]持续集成系列
持续集成一直很蛋疼,感觉没底. 几个方面来写 1,搭建gitlab 配邮箱 域名等使之好用 2,搭建jenkins –yum,安装常见插件 3,搭建sonar,汉化 4,安装sonar-scanner ...
- CONFIG_*头文件的配置
通常在kernel或uboot中, 有很多以CONFIG_*开头的宏配置选项,并且保存在相应的头文件中,那么这些CONFIG_*是怎么生成的呢? 在uboot的顶层Makefile中,有这么一项: 此 ...
- ajax查找错误信息
error: function(XMLHttpRequest, textStatus, errorThrown) { alert(XMLHttpRequest.status); alert(XMLHt ...
- 转:C++ 关键字 inline详细介绍
1. 内联函数 在C++中我们通常定义以下函数来求两个整数的最大值: int max(int a, int b) { return a > b ? a : b; } 为这么一个小的操作定义一个 ...
- int和Integer差别
种原始数据类型之中的一个. Java为每一个原始类型提供了封装类.Integer是java为int提供的封装类. 原始数据类型包含byte.int.char.long.float.double.boo ...
- VLC WebPlugin中文
Documentation:WebPlugin 这篇文档讲述的是 VLC media player Web plugins 和怎样在网页使用它 Contents 1 介绍: 构建包含video的Web ...
- htm5 俄罗斯方块
<!DOCTYPE html> <html manifest="tetris.manifest"> <!--在HTML标签里manifest=”cac ...