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 ...
随机推荐
- MySQL Memory 存储引擎浅析
原创文章,转载必需注明出处:http://www.cnblogs.com/wu-jian/ 前言 需求源自项目中的MemCache需求,開始想用MemCached(官方网站:http://memcac ...
- atitit.跨语言执行cmd cli api的原理及兼容性设计草案
atitit.跨语言执行cmd cli api的原理及兼容性设计草案 1. 标准输入,标准输出,标准错误与重新定向1 2. 常见问题2 2.1. 执行bat文件2 2.2. 执行bat文件 /c ...
- PHP 7安装使用体验,升级PHP要谨慎
一.发挥PHP 7高性能的几个要点 PHP 7相对于之前的PHP版本来说可以说性能有了质的飞跃,但是所谓“好马配好鞍,好车配风帆”,想要发挥PHP 7的性能优势,还需要从以下几个方面做准备:(此部分引 ...
- openWRT自学---针对backfire版本的主要目录和文件的作用的分析整理
特别说明:要编译backfire版本,一定要通过svn下载:svn co svn://svn.openwrt.org/openwrt/branches/backfire,而不能使用http://dow ...
- 华为终端-新浪微博联合创新,3D建模+AR 成就全新社交体验
近日,全球首款搭载3D感知摄像头的手机华为Mate 20发布. 通过Mate 20自带的景深摄像头及麒麟980的NPU加速能力,手机能够在获取物体表面信息后,完成高速的精细化3D建模. 那么,如何让3 ...
- Elasticsearch5.X IN Windows 10 系列文章(2)
ElasticSearch版本: 5.5.1 (最新稳定版为5.5.2),由于用到IK中文分词插件,最新版本没有5.5.2 ,所以使用5.5.1 日期:2017-08-29 第二章:安装Kibana ...
- Oracle常见死锁发生的原因以及解决方法
Oracle常见死锁发生的原因以及解决办法 一,删除和更新之间引起的死锁 造成死锁的原因就是多个线程或进程对同一个资源的争抢或相互依赖.这里列举一个对同一个资源的争抢造成死锁的实例. Oracle 1 ...
- #进阶系列——WebApi 身份认证解决方案:Basic基础认证
阅读目录 一.为什么需要身份认证 二.Basic基础认证的原理解析 1.常见的认证方式 2.Basic基础认证原理 三.Basic基础认证的代码示例 1.登录过程 2./Home/Index主界面 3 ...
- Javascript间歇调用和超时调用
间歇调用:每隔指定的时间就执行一次代码 超时调用:在指定的时间过后执行代码 HTML Code 12345678910111213141516171819 <!DOCTYPE html& ...
- sql server 2008 对字段的操作
添加,刪除字段 通用式: alter table [表名] add [字段名] 字段属性 default 缺省值 default 是可选参数 增加字段: 增加数字字段,整型,缺省值为0 增加数字 ...