PAT A 1002. A+B for Polynomials
2015-08-09 20:39
369 查看
原题:
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
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.Scanner; public class Pat1002{ public static void main(String args[]){ Scanner input = new Scanner(System.in); int n = input.nextInt(); int[] a1 = new int ; double[] a2 = new double ; for (int i = 0; i < n; i++){ a1[i] = input.nextInt(); a2[i] = input.nextDouble(); } int m = input.nextInt(); int[] b1 = new int[m]; double[] b2 = new double[m]; for (int i = 0; i < m; i++){ b1[i] = input.nextInt(); b2[i] = input.nextDouble(); } int i = 0; int j = 0; int count = 0; int count1 = a1[i]; int count2 = b1[j]; String c = ""; while(count1 != -1 || count2 != -1){ if (count1 > count2){ c = c + " " + count1 + " " + Math.round(a2[i] * 10) / 10.0; //保留一位小数 count++; if (i < a1.length - 1) count1 = a1[++i]; else count1 = -1; } else if (count1 < count2){ c = c + " " + count2 + " " + Math.round(b2[j] * 10) / 10.0; count++; if (j < b1.length - 1) count2 = b1[++j]; else count2 = -1; } else{ double sum = Math.round(((a2[i] + b2[j]) * 10)) / 10.0; if (sum != 0){ //某项被消除的情况 c = c + " " + count1 + " " + sum; count++; } if (i < a1.length - 1) count1 = a1[++i]; else count1 = -1; if (j < b1.length - 1) count2 = b1[++j]; else count2 = -1; } } if (c == "") //相加为0的情况 c = "0"; else c = count + c; System.out.print(c); input.close(); } }
相关文章推荐
- 大数相乘、大数相加、大数相减Java版本
- web.xml的组成理解
- Java Serializable(序列化)的理解和总结
- Java栈的实例模拟
- SSH三大框架整合(Hibernate,Spring,struts2)
- 华为机试——两个超长正整数的加法 java
- 智渔课堂官方免费教程四:Java基础之数据类型 常量 变量 数据类型转换
- I学霸官方免费教程四:Java基础之数据类型 常量 变量 数据类型转换
- java线层的启动与停止
- Java实现算法之--选择排序
- Java基础学习总结---------异常Exception(2)
- Java产生随机数
- 栈的链表实现(JAVA)
- java面向接口编程
- Java程序设计求出岁数
- 如何清楚描述spring
- 关于 java 实现 语音朗读
- 通过网页访问SQL数据库,提示java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
- spring的@Transactional注解详细用法
- Java字符串题目--删除给定字符串中出现次数最少的字符