1002 A+B for Polynomials (25 分)
This time, you are supposed to find A+B where A and B are two polynomials.
Input Specification:
Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:
?
where K is the number of nonzero terms in the polynomial, and (i =1,2,...,k) are the exponents and coefficients, respectively. It is given that
Output Specification:
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
1002 多項式 A + B (25 分)
這一次冤竹,你應(yīng)該找到A+B岭皂,其中A和B是兩個多項式权悟。
輸入規(guī)格
每個輸入文件包含一個測試用例脓规。每個案例占2行,每一行包含一個多項式的信息:?其中K是多項式中的非零項數(shù)禁灼, and (i=1谤绳,2,…塌计,k)分別是指數(shù)和系數(shù)挺身。它被限制為
輸出規(guī)格
對于每個測試用例,您應(yīng)該在一行中輸出A和B的和锌仅,其格式與輸入相同章钾。請注意,每一行的末尾必須沒有額外的空間热芹。請精確到小數(shù)點后1位贱傀。
輸入樣例
2 1 2.4 0 3.2
2 2 1.5 1 0.5
輸出樣例
3 2 1.5 1 2.9 0 3.2
代碼
import java.util.Scanner;
public class Main {
private static Scanner sc;
public static void main(String[] args) {
// TODO Auto-generated method stub
float[] arr = new float[1001];
int a = 0;
int b = 0;
int c = 0;
int tem;
sc = new Scanner(System.in);
a = sc.nextInt();
for (int i = 0; i < a; i++) {
tem = sc.nextInt();
arr[tem] += sc.nextFloat();
}
b = sc.nextInt();
for (int i = 0; i < b; i++) {
tem = sc.nextInt();
arr[tem] += sc.nextFloat();
}
sc.close();
for (int i = 0; i < 1001; i++) {
if (arr[i] != 0) {
c++;
}
}
System.out.print(c);
for (int i = 1000; i >=0; i--) {
if (arr[i] != 0.0) {
System.out.format(" %d %.1f", i,arr[i]);
//System.out.print(" " + i + " " + arr[i]);
}
}
}
}