PAT1002 A+B for Polynomials

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String[] line1 = reader.readLine().split(" ");
        String[] line2 = reader.readLine().split(" ");
        Map data1 = new HashMap<>();
        List indexs = new ArrayList<>();
        for (int i = 1; i < line1.length; i = i + 2) {
            indexs.add(Integer.parseInt(line1[i]));
            data1.put(Integer.parseInt(line1[i]), Double.parseDouble(line1[i + 1]));
        }
        Map data2 = new HashMap<>();
        for (int i = 1; i < line2.length; i = i + 2) {
            if (!indexs.contains(Integer.parseInt(line2[i]))) {
                indexs.add(Integer.parseInt(line2[i]));
            }
            data2.put(Integer.parseInt(line2[i]), Double.parseDouble(line2[i + 1]));
        }
        Collections.sort(indexs);
        int count = 0;
        String result = "";
        for (int i = indexs.size() - 1; i >= 0; i--) {
            int index = indexs.get(i);
            double value1 = data1.get(index) == null ? 0.0f : data1.get(index);
            double value2 = data2.get(index) == null ? 0.0f : data2.get(index);
            if (value1 + value2 != 0) {
                count = count + 1;
                result = result + " " + index;
                result = result + " " + String.format("%.1f", value1 + value2);
            }
        }
        System.out.print(count);
        if (!result.isEmpty()) {
            System.out.print(result);
        }
    }

}

你可能感兴趣的:(algorithm)