zoukankan      html  css  js  c++  java
  • 一元多项式的乘法与加法运算

    一元多项式的乘法与加法运算

    设计函数分别求两个一元多项式的乘积与和。

    输入格式:

    输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。

    输出格式:

    输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0

    输入样例:

    4 3 4 -5 2  6 1  -2 0
    3 5 20  -7 4  3 1
    

    输出样例:

    15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1
    5 20 -4 4 -5 2 9 1 -2 0
    #include <iostream>
    #include<cstdio>
    #include<cstring>
    #include<map>
    #include<set>
    #include<queue>
    #include<vector>
    #include<deque>
    #include<algorithm>
    using namespace std;
    int n,m,k;
    int num,l;
    int ans[35];
    struct node
    {
        int x,y;
        node(int aa,int bb){x=aa; y=bb;}
    };
    struct nod
    {
        int x,y;
    }a[2005],b[2005];
    struct cmp
    {
        bool operator()(node a,node b)
        {
            return a.y<b.y;
        }
    };
    int main()
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
         scanf("%d%d",&a[i].x,&a[i].y);
        scanf("%d",&m);
        for(int i=1;i<=m;i++)
         scanf("%d%d",&b[i].x,&b[i].y);
        priority_queue<node,vector<node>,cmp> Q;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
        {
            Q.push(node(a[i].x*b[j].x,a[i].y+b[j].y));
        }
        int l=0;
        while(!Q.empty())
        {
           node u=Q.top();
           Q.pop();
           if (!Q.empty())
           {
               node v=Q.top();
              while (u.y==v.y)
             {
                 u.x+=v.x;
                 Q.pop();
                 if (Q.empty()) break;
                 v=Q.top();
             }
           }
          if (u.x==0) continue; // 如果系数为0的话,不输出
          if (l++) printf(" ");
             printf("%d %d",u.x,u.y);
        }
        if(l==0) printf("0 0"); //如果一个都没有输出,则输出“0 0”
        printf("
    ");
        for(int i=1;i<=n;i++) Q.push(node(a[i].x,a[i].y));
        for(int j=1;j<=m;j++) Q.push(node(b[j].x,b[j].y));
         l=0;
        while(!Q.empty())
        {
           node u=Q.top();
           Q.pop();
           if (!Q.empty())
           {
               node v=Q.top();
              while (u.y==v.y)
             {
                 u.x+=v.x;
                 Q.pop();
                 if (Q.empty()) break;
                 v=Q.top();
             }
           }
          if(u.x==0) continue;
          if (l++) printf(" ");
             printf("%d %d",u.x,u.y);
        }
        if(l==0) printf("0 0");
        printf("
    ");
       return 0;
    }
  • 相关阅读:
    自动机
    C语言文法
    实验报告一:词法分析
    Python的基础综合练习
    Python基础综合练习
    turtle画五星红旗
    熟悉常用的Linux操作
    大数据概述
    对学习编译原理的看法
    LINUX
  • 原文地址:https://www.cnblogs.com/stepping/p/6530979.html
Copyright © 2011-2022 走看看