AcWing 1474. 多项式 A + B-java
原题链接
简单
作者:
Astarion
,
2021-03-08 15:25:41
,
所有人可见
,
阅读 326
import java.io.*;
class Main {
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(isr);
OutputStreamWriter osw = new OutputStreamWriter(System.out);
BufferedWriter out = new BufferedWriter(osw);
int cnt = 0;
double[] as = new double[1010];
String[] strs = in.readLine().split(" ");
int k1 = Integer.parseInt(strs[0]);
for (int i = 0; i < k1; i++) {
int n = Integer.parseInt(strs[2 * i + 1]);
double a = Double.parseDouble(strs[2 * i + 2]);
if (as[n] == 0) {
cnt++;
}
as[n] += a;
}
strs = in.readLine().split(" ");
int k2 = Integer.parseInt(strs[0]);
for (int i = 0; i < k2; i++) {
int n = Integer.parseInt(strs[2 * i + 1]);
double a = Double.parseDouble(strs[2 * i + 2]);
if (as[n] == 0) {
cnt++;
}
as[n] += a;
}
out.write(cnt + "");
for (int i = 1001; i >= 0; i--) {
if (as[i] != 0) {
out.write(String.format(" %d %.1f", i, as[i]));
}
}
in.close();
isr.close();
out.flush();
out.close();
osw.close();
}
}