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 Input2 1 2.4 0 3.2 2 2 1.5 1 0.5Sample Output
3 2 1.5 1 2.9 0 3.2
import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String input1 = scanner.nextLine(); String[] arr1 = input1.split(" "); int n1 = Integer.parseInt(arr1[0]); String input2 = scanner.nextLine(); String[] arr2 = input2.split(" "); int n2 = Integer.parseInt(arr2[0]); Map<Integer, Double> resultMap = new LinkedHashMap<Integer, Double>(); int i = 1, j = 1; while(true){ if(i > n1 * 2 && j > n2 * 2){ break; } if(i == n1 * 2 + 1){ int exp2 = Integer.parseInt(arr2[j]); double coef2 = Double.parseDouble(arr2[j + 1]); resultMap.put(exp2, coef2); j += 2; } else if(j == n2 * 2 + 1){ int exp1 = Integer.parseInt(arr1[i]); double coef1 = Double.parseDouble(arr1[i + 1]); resultMap.put(exp1, coef1); i += 2; } else { int exp1 = Integer.parseInt(arr1[i]); double coef1 = Double.parseDouble(arr1[i + 1]); int exp2 = Integer.parseInt(arr2[j]); double coef2 = Double.parseDouble(arr2[j + 1]); if(exp1 == exp2){ resultMap.put(exp1, coef1 + coef2); i += 2; j += 2; } else if(exp1 > exp2){ resultMap.put(exp1, coef1); i += 2; } else { resultMap.put(exp2, coef2); j += 2; } } } StringBuilder builder = new StringBuilder(); builder.append(resultMap.size()); for(Iterator<Integer> it = resultMap.keySet().iterator(); it.hasNext();){ int exp = it.next(); builder.append(" ").append(exp); builder.append(" ").append(resultMap.get(exp)); } System.out.println(builder.toString()); } }
评测结果
时间 | 结果 | 得分 | 题目 | 语言 | 用时(ms) | 内存(kB) | 用户 |
---|---|---|---|---|---|---|---|
4月30日 09:38 | 部分正确 | 15 | 1002 | Java (javac 1.6.0) | 80 | 10644 | zspring |
测试点
测试点 | 结果 | 用时(ms) | 内存(kB) | 得分/满分 |
---|---|---|---|---|
0 | 答案正确 | 79 | 10628 | 13/13 |
1 | 答案错误 | 80 | 10500 | 0/2 |
2 | 答案正确 | 80 | 10516 | 2/2 |
3 | 答案错误 | 79 | 10628 | 0/2 |
4 | 答案错误 | 80 | 10516 | 0/2 |
5 | 答案错误 | 80 | 10644 | 0/2 |
6 | 答案错误 | 79 | 10500 | 0/2 |