1009. Product of Polynomials (25)
时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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 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.
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
提交代码
当觉得主要的算法没有问题时,花点时间去关注一些关键变量的变化规律,比如这里的num。
1 #include <cstdio> 2 #include <cstring> 3 #include <string> 4 #include <queue> 5 #include <stack> 6 #include <iostream> 7 #include <cmath> 8 using namespace std; 9 #define exp 1e-9 10 struct node{ 11 int e; 12 double c; 13 node *next; 14 }; 15 int main(){ 16 //freopen("D:\\INPUT.txt","r",stdin); 17 node *a,*b,*head; 18 head=new node(); 19 head->next=NULL; 20 int na; 21 scanf("%d",&na); 22 int i; 23 a=new node[na]; 24 for(i=0;i<na;i++){ 25 scanf("%d %lf",&a[i].e,&a[i].c); 26 } 27 int nb; 28 scanf("%d",&nb); 29 b=new node[nb]; 30 for(i=0;i<nb;i++){ 31 scanf("%d %lf",&b[i].e,&b[i].c); 32 } 33 int j,num=0; 34 for(i=0;i<na;i++){ 35 for(j=0;j<nb;j++){ 36 int e=a[i].e+b[j].e; 37 double c=a[i].c*b[j].c; 38 node *q=head,*t=head->next; 39 while(t){ 40 if(t->e<e){//q->e>=e 41 break; 42 } 43 q=t; 44 t=q->next; 45 } 46 if(q!=head&&q->e==e){ 47 q->c+=c; 48 } 49 else{ 50 t=new node(); 51 t->c=c; 52 t->e=e; 53 t->next=q->next; 54 q->next=t; 55 } 56 } 57 } 58 node *p; 59 p=head->next; 60 while(p){//系数可能为0!! 这里注意!! 61 if(abs(p->c)>exp) 62 num++; 63 p=p->next; 64 } 65 p=head->next; 66 printf("%d",num); 67 while(p){ 68 if(abs(p->c)>exp)//系数可能为0!! 69 printf(" %d %.1lf",p->e,p->c); 70 head->next=p->next; 71 delete p; 72 p=head->next; 73 } 74 printf("\n"); 75 delete []a; 76 delete []b; 77 delete head; 78 return 0; 79 }