第五天
A1009 Product of 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:
K N a N a... Na
where K is the number of nonzero terms in the polynomial, Nand a(i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10,0≤N<⋯<N<N≤1000.
Output Specification:
For each test case you should output the product 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 up to 1 decimal place.
Sample Input:
2 1 2.4 0 3.2
2 2 1.5 1 0.5
Sample Output:
3 3 3.6 2 6.0 1 1.6
单词
product
英 /'prɒdʌkt/ 美 /'prɑdʌkt/
n. 产品;结果;[数] 乘积;作品
题目分析
多项式相乘,类似于A1002的多项式相加,之前也写过用的开结构体数组的方法,很笨,所以用之前在A1002学到的开一个数组,用下标代表指数,对应元素代表系数的方式来存,不过因为是乘法所以要多开两个数组把数据临时保存一下。代码如下。
具体代码
#include<stdio.h> #include<stdlib.h> double p1[1001]; double p2[1001]; double res[2002]; int N, M; int main(void) { scanf("%d", &N); for (int i = 0; i < N; i++) { int e; double c; scanf("%d %lf", &e, &c); p1[e] = c; } scanf("%d", &M); for (int i = 0; i < M; i++) { int e; double c; scanf("%d %lf", &e, &c); p2[e] = c; } for (int i = 0; i < 1001; i++) { if (p1[i] != 0) for (int j = 0; j < 1001; j++) if (p2[j] != 0) res[i + j] += p1[i] * p2[j]; } int num = 0; for (int i = 0; i < 2002; i++) { if (res[i] != 0) num++; } printf("%d", num); for (int i = 2001; i >= 0; i--) { if (res[i] != 0) printf(" %d %0.1f", i, res[i]); } system("pause"); }