1009. Product of Polynomials
来源:互联网 发布:淘宝上的上海华硕商城 编辑:程序博客网 时间:2024/05/22 07:00
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 Input2 1 2.4 0 3.22 2 1.5 1 0.5Sample Output
3 3 3.6 2 6.0 1 1.6
#include <cstdio>
#include <cstdlib>
#include<iostream>
#include <deque>
#include <queue>
#include <string>
#include <vector>
#include <iomanip>
#include<algorithm>
using namespace std;
#define max1 501
#define inf 4500
typedef struct Node
{
int expon;
double coeff;
struct Node *next;
}*Lnode;
void attach(Lnode *p,int e,double c)
{
Lnode s;
s=(Lnode)malloc(sizeof(struct Node));
s->expon=e;
s->coeff=c;
s->next=NULL;
(*p)->next=s;
(*p)=s;
}
Lnode readPoly()
{
Lnode p,l;
p=(Lnode)malloc(sizeof(struct Node));
p->next=NULL;
l=p;
int n,e;
double c;
cin>>n;
while(n)
{
cin>>e;
cin>>c;
attach(&p,e,c);
n--;
}
p=l;l=l->next;free(p);
return l;
}
Lnode mutiPoly(Lnode t1,Lnode t2)
{
Lnode p,s,g,l2;
int e;
double c;
p=(Lnode)malloc(sizeof(struct Node));
p->next=NULL;
s=p;
l2=t2;
while(t2)
{
attach(&p,t1->expon+t2->expon,t1->coeff*t2->coeff);
t2=t2->next;
}
t1=t1->next;
while(t1)
{
t2=l2;
p=s;
while(t2)
{
e=t1->expon+t2->expon;
c=t1->coeff*t2->coeff;
while(p->next&&p->next->expon>e)
p=p->next;
if(p->next&&p->next->expon==e)
{
if(p->next->coeff+c)
p->next->coeff+=c;
else
{
g=p->next;
p->next=g->next;
free(g);
}
}
else
{
g=(Lnode)malloc(sizeof(struct Node));
g->expon=e;
g->coeff=c;
g->next=p->next;
p->next=g;
p=p->next;
}
t2=t2->next;
}
t1=t1->next;
}
p=s;s=s->next;free(p);
return s;
}
void Print(Lnode s)
{
Lnode p;
p=s;
int num=0;
while(p)
{
num++;
p=p->next;
}
cout<<num<<' ';
int flag=1;
while(s)
{
if(flag)
flag=0;
else
cout<<' ';
cout<<fixed<<setprecision(0)<<s->expon<<' ';
cout<<fixed<<setprecision(1)<<s->coeff;
s=s->next;
}
}
int main()
{
Lnode L1,L2,LL;
L1=readPoly();
L2=readPoly();
/*Print(L1);
cout<<'\n';
Print(L2);*/
LL=mutiPoly(L1,L2);
Print(LL);
return 0;
}
int expon;
double coeff;
struct Node *next;
}*Lnode;
void attach(Lnode *p,int e,double c)
{
Lnode s;
s=(Lnode)malloc(sizeof(struct Node));
s->expon=e;
s->coeff=c;
s->next=NULL;
(*p)->next=s;
(*p)=s;
}
Lnode readPoly()
{
Lnode p,l;
p=(Lnode)malloc(sizeof(struct Node));
p->next=NULL;
l=p;
int n,e;
double c;
cin>>n;
while(n)
{
cin>>e;
cin>>c;
attach(&p,e,c);
n--;
}
p=l;l=l->next;free(p);
return l;
}
Lnode mutiPoly(Lnode t1,Lnode t2)
{
Lnode p,s,g,l2;
int e;
double c;
p=(Lnode)malloc(sizeof(struct Node));
p->next=NULL;
s=p;
l2=t2;
while(t2)
{
attach(&p,t1->expon+t2->expon,t1->coeff*t2->coeff);
t2=t2->next;
}
t1=t1->next;
while(t1)
{
t2=l2;
p=s;
while(t2)
{
e=t1->expon+t2->expon;
c=t1->coeff*t2->coeff;
while(p->next&&p->next->expon>e)
p=p->next;
if(p->next&&p->next->expon==e)
{
if(p->next->coeff+c)
p->next->coeff+=c;
else
{
g=p->next;
p->next=g->next;
free(g);
}
}
else
{
g=(Lnode)malloc(sizeof(struct Node));
g->expon=e;
g->coeff=c;
g->next=p->next;
p->next=g;
p=p->next;
}
t2=t2->next;
}
t1=t1->next;
}
p=s;s=s->next;free(p);
return s;
}
void Print(Lnode s)
{
Lnode p;
p=s;
int num=0;
while(p)
{
num++;
p=p->next;
}
cout<<num<<' ';
int flag=1;
while(s)
{
if(flag)
flag=0;
else
cout<<' ';
cout<<fixed<<setprecision(0)<<s->expon<<' ';
cout<<fixed<<setprecision(1)<<s->coeff;
s=s->next;
}
}
int main()
{
Lnode L1,L2,LL;
L1=readPoly();
L2=readPoly();
/*Print(L1);
cout<<'\n';
Print(L2);*/
LL=mutiPoly(L1,L2);
Print(LL);
return 0;
}
0 0
- 1009. Product of Polynomials
- 1009. Product of Polynomials
- 1009. Product of Polynomials
- 1009.Product of Polynomials
- 1009. Product of Polynomials
- 1009. Product of Polynomials
- 1009. Product of Polynomials
- 1009. Product of Polynomials
- 1009. Product of Polynomials
- 1009. Product of Polynomials
- 1009. Product of Polynomials
- 1009. Product of Polynomials (25)
- 【PAT】1009. Product of Polynomials
- 1009. Product of Polynomials (25)
- 1009. Product of Polynomials (25)
- 1009. Product of Polynomials (25)
- 1009. Product of Polynomials (25)
- 1009. Product of Polynomials (25)
- Windows 10安装TensorFlow
- LinearLayout中组件右对齐
- 1248
- centos6.5vim基本配置
- 数据库索引系列二:什么情况增加索引
- 1009. Product of Polynomials
- bzoj 1355 kmp
- 17.2.12 B 组 seek
- 1250
- 3888
- 1010. Radix
- UVALive - 7527 Funfair (动态规划)
- This view has crashed Markdown的渲染问题
- 图论最短路径算法——SPFA