# 1009 Product of Polynomials

This time, you are supposed to find

### Input Specification:

Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:

where

### Output Specification:

For each test case you should output the product of

### 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

Code：

#include<iostream>
#include<algorithm>
#include<iomanip>

using namespace std;

typedef struct PolyNode *Polynomial;

struct PolyNode
{
double coef;
int expon;
};

void Attach(int expon, double coef, Polynomial* rear) {
Polynomial P = (Polynomial)malloc(sizeof(struct PolyNode));
P->expon = expon;
P->coef = coef;
(*rear) = P;
}

Polynomial p, pRear, t;
int e, N;
double c;
p = (Polynomial)malloc(sizeof(struct PolyNode));
cin >> N;
pRear = p;
while(N--) {
cin >> e >> c;
Attach(e, c, &pRear);
}
t = p, p = p->link, free(t);
return p;
}

Polynomial MultPoly(Polynomial P1, Polynomial P2) {
Polynomial Rear, t, t1, t2, P;
int e;
double c;

if (P1 == NULL || P2 == NULL) return NULL;

t1 = P1, t2 = P2;
P = (Polynomial)malloc(sizeof(struct PolyNode));
Rear = P;
while(t2) {
Attach(t1->expon + t2->expon, t1->coef * t2->coef, &Rear);
}

while (t1) {
t2 = P2; Rear = P;
while (t2) {
e = t1->expon + t2->expon;
c = t1->coef * t2->coef;
} else {
free(t);
}
} else {
t = (Polynomial)malloc(sizeof(struct PolyNode));
t->expon = e;
t->coef = c;
}

}
}

t = P; P = P->link; free(t);
return P;
}

void PrintPoly(Polynomial p) {
Polynomial t = p;
int len = 0;
while (t != NULL) {
len++;
}
cout << len;
while(p != NULL) {
cout << " " << p->expon;
cout << " " << fixed << setprecision(1) << p->coef;
}
}

int main() {
Polynomial p1, p2, pp;

pp = MultPoly(p1, p2);
PrintPoly(pp);

return 0;
}


1009 Product of Polynomials

(0)
(0)