1002. A+B for Polynomials

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.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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值