题目描述
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
AC代码
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
struct node{
int e; //指数
double c; //系数
}A[11], B[11];
int main(){
int k1, k2;
double C[10010] = {0}; //下标为指数,值为系数
int ex;
double co;
cin>>k1;
for(int i = 0; i < k1; i++)
cin>>A[i].e>>A[i].c;
cin>>k2;
for(int i = 0; i < k2; i++)
cin>>B[i].e>>B[i].c;
int max = A[0].e + B[0].e; //最大系数
for(int i = 0; i < k1; i++){
for(int j = 0; j < k2; j++){
ex = A[i].e + B[j].e;
co = A[i].c * B[j].c;
C[ex] += co;
}
}
int cnt = 0; //非零项数
for(int i = max; i >= 0; i--)
if(C[i] != 0) cnt++;
cout<<cnt;
for(int i = max; i >= 0; i--){
if(C[i] != 0)
printf(" %d %.1f", i, C[i]);
}
return 0;
}