PAT 1002. A+B for Polynomials (25)
2017-07-06 15:45
447 查看
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
1 import java.util.LinkedList; 2 import java.util.Queue; 3 import java.util.Scanner; 4 5 public class Main { 6 public static void main(String[] args) { 7 Queue<A> q1 = new LinkedList<A>(); 8 Queue<A> q2 = new LinkedList<A>(); 9 Queue<A> q3 = new LinkedList<A>(); 10 Scanner input = new Scanner(System.in); 11 int n = input.nextInt(); 12 for(int i=0;i<n;i++){ 13 A a = new A(); 14 a.a = input.nextInt(); 15 a.b = input.nextDouble(); 16 q1.add(a); 17 } 18 int m = input.nextInt(); 19 for(int i=0;i<m;i++){ 20 A a = new A(); 21 a.a = input.nextInt(); 22 a.b = input.nextDouble(); 23 q2.add(a); 24 } 25 while(!q1.isEmpty()&&!q2.isEmpty()){ 26 int a1 = q1.peek().a; 27 double b1 = q1.peek().b; 28 int a2 = q2.peek().a; 29 double b2 = q2.peek().b; 30 A a = new A(); 31 if(a1==a2){ 32 a.a = a1; 33 a.b = b1+b2; 34 q1.poll(); 35 q2.poll(); 36 }if(a1>a2){ 37 a.a = a1; 38 a.b = b1; 39 q1.poll(); 40 }if(a1<a2){ 41 a.a = a2; 42 a.b = b2; 43 q2.poll(); 44 } 45 if(a.b!=0) 46 q3.add(a); 47 } 48 while(!q1.isEmpty()){ 49 q3.add(q1.poll()); 50 } 51 while(!q2.isEmpty()){ 52 q3.add(q2.poll()); 53 } 54 System.out.print(q3.size()); 55 56 while(!q3.isEmpty()){ 57 System.out.print(" "+q3.peek().a+" "); 58 System.out.printf("%.1f",q3.peek().b); 59 q3.poll(); 60 } 61 62 } 63 } 64 class A{ 65 int a; 66 double b; 67 }
相关文章推荐
- PAT 1002. A+B for Polynomials (25)
- pat 1002. A+B for Polynomials (25)
- 【PAT】1002. A+B for Polynomials (25)
- PAT 1002. A+B for Polynomials (25)(多项式加法)(待修改)
- PAT-A-1002. A+B for Polynomials (25)
- 【PAT】1002. A+B for Polynomials(25)
- 【PAT】1002. A+B for Polynomials (25)
- 【PAT甲级】1002. A+B for Polynomials (25)
- PAT 1002. A+B for Polynomials (25)
- PAT (Advanced Level) 1002. A+B for Polynomials (25)
- PAT甲级 1002. A+B for Polynomials (25) 题目翻译与答案
- PAT - 甲级 - 1002. A+B for Polynomials (25)(模拟)
- PAT Advanced Level 1002. A+B for Polynomials (25)(Java)
- PAT (Advanced Level) 1002. A+B for Polynomials (25) 解题报告
- PAT-A 1002. A+B for Polynomials (25)
- PAT甲 1002. A+B for Polynomials (25)
- PAT甲级真题及训练集(3)--1002. A+B for Polynomials (25)
- PAT甲级1002. A+B for Polynomials (25)
- PAT 1002. A+B for Polynomials (25)
- 【PAT Advanced Level】1002. A+B for Polynomials (25)