码迷,mamicode.com
首页 > 其他好文 > 详细

1009 Product of Polynomials (25分)

时间:2021-01-16 12:09:45      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:ott   number   exponents   product   read   ica   his   term   input   

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:

N?1?? a?N?1???? N?2?? a?N?2???? ... N?K?? a?N?K????

where K is the number of nonzero terms in the polynomial, N?i?? and a?N?i???? (,) are the exponents and coefficients, respectively. It is given that 1, 0.

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
方法一:链表法(不建议使用
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<string.h>
using namespace std;
//多项式相乘与相加 
const int maxn=110010;

typedef struct Node{
    int expon;
    double coef;
    struct Node *next;    
}node,*pNode;

pNode ReadPoly(pNode p){
    int K;
    pNode q,r;
    scanf("%d",&K);
    p=new node;
    p->next=NULL;
    r=p;
    while(K--){
        q=new node;
        scanf("%d %lf",&q->expon,&q->coef);
        r->next=q;
        r=r->next;
    }
    r->next=NULL;
    return p;
} 
void printPoly(pNode p){
    pNode q,r;
    int k=0;
    r=p->next;
    while(r)
    {
        if(r->coef!=0)
        {
            k++;
        }
        r=r->next;
    }
    printf("%d ",k);
    q=p->next;
    while(q){
        if(q->coef==0){//系数为0,则不必输出 
            q=q->next;
            continue;
        } 
        if(q->next!=NULL)
        {
            printf("%d %.1f ",q->expon,q->coef);
        } 
        else{
            printf("%d %.1f",q->expon,q->coef);
        }
        q=q->next;
    }
    printf("\n");
}
pNode Mult(pNode p1,pNode p2){
    pNode ps,psn,ppp;
    ps=new node;//新链表头部 
    ps->expon=0;
    ps->coef=0;
    ps->next=NULL;
//    psn=pp;//新链表尾部
    pNode p11,p22;
    p11=p1->next;
    int flog=0;
//    p22=p2->next;
    while(p11){
         p22=p2->next;//返回链表第一个结点 
        while(p22){
            ppp=new node;
            ppp->expon=p11->expon+p22->expon;
            ppp->coef=p11->coef*p22->coef;
            psn=ps;//返回链表头节点 
            flog=0;
            while(psn->next)
            {
                if(psn->next->expon==ppp->expon){
                    flog=1;
                    break;
                }
                else{
                    psn=psn->next;
                }
            } 
            if(flog==1){
                psn->next->expon=ppp->expon;
                psn->next->coef=psn->next->coef+ppp->coef;//注意不要写错哦 
            }
            else{
                psn->next=ppp;
                psn=psn->next;
                psn->next=NULL;
            }
            p22=p22->next;
        }
        p11=p11->next;
    } 
    return ps;
    
} 
pNode Add(pNode p1,pNode p2){
    pNode ps,pp1,ppp;//pp指向链表头部,pp1指向链表尾部,ppp指向新节点 
    pNode p11,p22;//p11,p22都指向链表尾部 
    p11=p1->next;
    p22=p2->next;
    ps=new node;
    pp1=ps;
    while(p11&&p22){
        ppp=new node;
        if(p11->expon>p22->expon){
            ppp->expon=p11->expon;
            ppp->coef=p11->coef;
            pp1->next=ppp;
            pp1=pp1->next;
            p11=p11->next;
        }
        else if(p11->expon<p22->expon){
            ppp->expon=p22->expon;
            ppp->coef=p22->coef;
            pp1->next=ppp;
            pp1=pp1->next;
            pp1->next=NULL;
            p22=p22->next;
        }
        else if(p11->expon==p22->expon){
            ppp->expon=p11->expon;
            ppp->coef=p11->coef+p22->coef;
            pp1->next=ppp;
            pp1=pp1->next;
            p11=p11->next;
            p22=p22->next; 
        }
    } 
    if(p11!=NULL){
        pp1->next=p11;
    }
    if(p22!=NULL){
        pp1->next=p22;
    }
    return ps;
}
pNode sort(pNode p){//冒泡排序 
    pNode r,t;
    int ti;
    double td;
    r=p->next;
    while(r){
        t=r->next;
        while(t){
            if(t->expon>r->expon){
            ti=t->expon;
            td=t->coef;
            t->expon=r->expon;
            t->coef=r->coef;
            r->expon=ti;
            r->coef=td;            
        }
        t=t->next; 
        }
        r=r->next;
    }
    return p;
}
int main(){
    pNode p1,p2,pp,ps;
    p1=ReadPoly(p1);
    p2=ReadPoly(p2);
    pp=Mult(p1,p2);
    pp=sort(pp);
    printPoly(pp);
//    ps=Add(p1,p2);
//    pp=sort(pp);
//    printPoly(ps);
    return 0;
}

方法二:利用数组

 

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<vector> 
#include<string.h>
using namespace std;
const int maxn=101000;
struct node{
    int expon;
    double coef;
};
bool compare(node a,node b){
    return a.expon>b.expon;
}
int main(){
    vector<node> p1,p2,p3;
    int m,n,e;
    node nd;
    double c;
    scanf("%d",&m);
    while(m--){
        scanf("%d %lf",&nd.expon,&nd.coef);
        p1.push_back(nd);
    }
    scanf("%d",&n);
    while(n--){
        scanf("%d %lf",&nd.expon,&nd.coef);
        p2.push_back(nd);
    }
    node p;
    int flog=0,k;
    for(int i=0;i<p1.size();i++){
        for(int j=0;j<p2.size();j++){
            p.expon=p1[i].expon+p2[j].expon;
            p.coef=p1[i].coef*p2[j].coef;
            flog=0;
            for(k=0;k<p3.size();k++){
                if(p3[k].expon==p.expon){
                    flog=1;
                    p3[k].coef=p3[k].coef+p.coef;
                    break;
                }
            }
            if(flog==0){
                p3.push_back(p);
            }
        }
    }
    sort(p3.begin(),p3.end(),compare);
    int t=0;
    for(int i=0;i<p3.size();i++){
        if(p3[i].coef!=0){
            t++;
        } 
    } 
    printf("%d ",t);
    for(int i=0;i<p3.size();i++){
        if(p3[i].coef==0){
            continue;
        }
        if(i<p3.size()-1)
        printf("%d %.1f ",p3[i].expon,p3[i].coef);
        else{
            printf("%d %.1f",p3[i].expon,p3[i].coef);
        }
    }
    printf("\n");
    return 0;
}

 

1009 Product of Polynomials (25分)

标签:ott   number   exponents   product   read   ica   his   term   input   

原文地址:https://www.cnblogs.com/dre-52yao/p/14284553.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!