A1009. Product of Polynomials
题目描述
This time, you are supposed to find A*B where A and B are two polynomials.
输入格式
Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N1 aN1 N2 aN2 ... NK aNK , where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1,2,...,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10, 0≤NK < ... < N2 < N1 ≤ 1000
输出格式
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
输入样例
2 1 2.4 0 3.2
2 2 1.5 1 0.5
输出样例
3 3 3.6 2 6.0 1 1.6
题意
给出两个多项式的系数,求这两个多项式的乘积
样例解释
已知第一个多项式为 f(x)=2.4x + 3.2,第二个多项式 g(x)=1.5x2 + 0.5x,F(x)*G(x)=3.6x3 + 6x2 + 1.6x
#include <bits/stdc++.h> struct Poly{ int exp; // 指数 double cof;// 系数 }poly[1001]; double ans[2001];// 存放结果 int main(int argc, char *argv[]) { int n, m, number = 0; scanf("%d", &n);// 第一个多项式中非零系数的项数 for(int i = 0; i < n; i++){ scanf("%d %1f", &poly[i].exp, &poly[i].cof);// 第一个多项式的指数和系数 } scanf("%d", &m);// 第二个多项式中非零系数的项数 for(int i = 0; i < m; i++){ int exp; double cof; scanf("%d %1f", &exp, &cof);// 第二个多项式的指数和系数 for(int j = 0; j < n; j++){ ans[exp + poly[j].exp] += (cof*poly[j].cof); } } for(int i = 0; i <= 2000; i++){ if(ans[i] != 0.0){ number++;// 累计非零系数的项数 } } printf("%d", number); // 从高次幂到低次幂输出 for(int i = 2000; i >= 0; i--) { if(ans[i] != 0.0]){ printf(" %d %.1f", i, ans[i]); } return 0; }
题解关键
- 指数相加,系数相乘
睁开眼,书在面前 闭上眼,书在心里