• 1009 Product of Polynomials (25分)


    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:

    N1​​ aN1​​​​ N2​​ aN2​​​​ ... NK​​ aNK​​​​

    where K is the number of nonzero terms in the polynomial, Ni​​ and aNi​​​​ (,) 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("
    ");
    }
    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("
    ");
        return 0;
    }
  • 相关阅读:
    输入一个1-9的数i,再输入一个数字n,表示 i 出现的次数,输入的2个数字 i 和 n 组合成如下表达式:如i=2,n=4,2+22+222+2222=?,计算结果是多少?
    现有数列1/2;2/3;3/5;5/8······第十次出现的是什么?
    猜数游戏:范围时1-100,若错误就提示大了还是小了,猜对则结束,允许猜10次,游戏结束后对玩家评价:1次猜对;5次内猜对;10次内猜对;没有猜对
    登录模拟,用户名和密码输入错误后给出相关错误提示,并告知还有多少次错误机会,如果5次验证失败将冻结账户
    30人围坐轮流表演节目,按顺序数1-3,每次数到3的人就表演节目,表演过的人不再参加报数,那么在仅剩一个人没有表演的时候,共报数多少人次?
    docker 自定义镜像
    php 镜像richarvey/nginx-php-fpm的ngnix配置
    php tp5常用小知识
    php Tp5下mysql的增删改查
    php 面试常问问题
  • 原文地址:https://www.cnblogs.com/dreamzj/p/14284553.html
Copyright © 2020-2023  润新知