zoukankan      html  css  js  c++  java
  • poj1996

    多项式计算,有点像母函数,注意最后要判断最高次项是否为0。

    #include <cstdio>
    #include <cstring>
    using namespace std;
    
    #define MAX_NUM 105
    
    int a_num, b_num;
    int a[MAX_NUM], b[MAX_NUM];
    int c[MAX_NUM * MAX_NUM];
    int poly[2][MAX_NUM * MAX_NUM];
    int poly_num;
    
    void input()
    {
        scanf("%d%d", &a_num, &b_num);
        for (int i = 0; i <= a_num; i++)
            scanf("%d", &a[i]);
        for (int i = 0; i <= b_num; i++)
            scanf("%d", &b[i]);
    }
    
    void add_c(int poly[], int a)
    {
        for (int i = 0; i <= poly_num; i++)
            c[i] += a * poly[i];
    }
    
    void add_poly(int last[], int next[])
    {
        memset(next, 0, sizeof(int) * MAX_NUM * MAX_NUM);
        for (int i = 0; i <= b_num; i++)
        {
            for (int j = 0; j <= poly_num; j++)
                next[i + j] += b[i] * last[j];
        }
        poly_num += b_num;
    }
    
    void work()
    {
        memset(c, 0, sizeof(c));
        for (int i = 0; i <= b_num; i++)
        {
            poly[1][i] = b[i];
        }
        poly_num = b_num;
        c[0] = a[0];
        add_c(poly[1], a[1]);
        for (int i = 2; i <= a_num; i++)
        {
            add_poly(poly[(i - 1) & 1], poly[i & 1]);
            add_c(poly[i & 1], a[i]);
        }
        while (c[poly_num] == 0)
            poly_num--;
        printf("%d", c[0]);
        for (int i = 1; i <= poly_num; i++)
            printf(" %d", c[i]);
        puts("");
    }
    
    int main()
    {
        int case_num;
        scanf("%d", &case_num);
        for (int i = 0; i < case_num; i++)
        {
            input();
            work();
        }
    }
    View Code
  • 相关阅读:
    Ubuntu 18.04安装gcc、g++ 4.8
    Java 接口返回值集合防止空指针
    Linux CentOS7.9环境下搭建Java Web 环境
    Springboot集成UReport2
    linux 环境中 单独执行 python 脚本
    sql 注入的问题
    检验上传文件的大小
    Gunicorn使用讲解
    CentOS下安装部署对象存储服务MinIO
    阿里云CentOS7安装MySQL
  • 原文地址:https://www.cnblogs.com/rainydays/p/3325386.html
Copyright © 2011-2022 走看看