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 ...
随机推荐
- 04-2winPE里面下载系统并安装系统教程
winPE里面下载系统并安装系统教程 首先需要注意的是,你已经使用 装机助理 工具制作好U盘启动盘,通过电脑功能键进入PE界面(不同型号的电脑进入PE界面的功能键不同,根据不同搜索属于她的功能键): ...
- python 在Windows中描述路径时出现的问题
问题的根本:windows读取文件可以用\,但在字符串里面\被作为转义字符使用, python在描述路径时有两种方式: 'd:\\a.txt',转义的方式 r'd:\a.txt',声明字符串不需要 ...
- Nginx 0.8.x + PHP 5.2.13(FastCGI)搭建胜过Apache十倍的Web服务器(第6版)(转)
转自:http://blog.s135.com/nginx_php_v6/] 前言:本文是我撰写的关于搭建“Nginx + PHP(FastCGI)”Web服务器的第6篇文章.本系列文章作为国内最早详 ...
- Atitit.互联网 软件编程 数据库方面 架构 大牛 牛人 attilax总结
Atitit.互联网 软件编程 数据库方面 架构 大牛 牛人 attilax总结 Coolshell 称号.理论与c++ 阮一峰:: 理论高手与js高手 王银:理论高手 赵劼,网名老赵,c#高手 与理 ...
- CSS3 :nth-child()伪类选择器
CSS3 :nth-child()伪类选择器 CSS3的强大,让人惊叹,人们在惊喜之余,又不得不为其艰难的道路感到可惜:好 的标准只有得到行业浏览器的良好支持才算得上“标准”.CSS3标 准已提出数年 ...
- 1. Two Sum【easy】
1. Two Sum[easy] Given an array of integers, return indices of the two numbers such that they add up ...
- char device
/** * alloc_chrdev_region() - register a range of char device numbers * @dev: output parameter for f ...
- jquery的text()
jQuery 文档操作 - text() 方法 jQuery 文档操作参考手册 实例 设置所有 <p> 元素的内容: $(".btn1").click(function ...
- nginx服务器部署
nginx(“engine x”)是一个高性能的HTTP和反向代理服务器. 安装nginx Linux下 sudo apt-get install nginx windows下 下载 nginx ...
- 让你的程序通过XP防火墙
procedure TForm1.Button1Click(Sender: TObject); var FwMgr,Profile,FwApp: variant; begin FwMgr := Cre ...