PAT 甲级 1009 Product of Polynomials (25 分)(Java)
题目
题目链接
题意解读
大体题意是两个多项式相乘
解题思路
首先保存第一个多项式的指数和系数,第二个多项式无需保存,输入的时候直接进行计算即可,然后结果通过数组的方式保存,数组下标即是指数,数组存储的值就是系数,这里最大也就是指数为2000,所以数组开到2001即可;
解法
解法一
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] exp = new int[1001];
double[] coe = new double[1001];
double[] resExp = new double[2001];
int m = sc.nextInt();
for (int i = 0; i < m; i++) {
exp[i] = sc.nextInt();
coe[i] = sc.nextDouble();
}
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
int e = sc.nextInt();
double c = sc.nextDouble();
for (int j = 0; j < m; j++) {
int index = e + exp[j];
resExp[index] += coe[j] * c;
}
}
int total = 0;
for (int i = 0; i < 2001; i++) {
if(resExp[i] != 0.0){
total++;
}
}
System.out.print(total);
for (int i = 2000; i >= 0; i--) {
if(resExp[i] != 0.0){
System.out.print(" " + i + " " + String.format("%.1f", resExp[i]));
}
}
}
}
|