zoukankan      html  css  js  c++  java
  • A1009. Product of Polynomials

    题目描述

      This time, you are supposed to find A*B where A and B are two polynomials.

    输入格式

      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

    输出格式

      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

    输入样例

    2 1 2.4 0 3.2

    2 2 1.5 1 0.5

    输出样例

    3 3 3.6 2 6.0 1 1.6

    题意

    给出两个多项式的系数,求这两个多项式的乘积

    样例解释

    已知第一个多项式为 f(x)=2.4x + 3.2,第二个多项式 g(x)=1.5x2 + 0.5x,F(x)*G(x)=3.6x3 + 6x2 + 1.6x

    #include <bits/stdc++.h>
    struct Poly{
        int exp; // 指数
        double cof;// 系数 
    }poly[1001];
    double ans[2001];// 存放结果 
    int main(int argc, char *argv[]) {
        int n, m, number = 0;
        scanf("%d", &n);// 第一个多项式中非零系数的项数 
        for(int i = 0; i < n; i++){
            scanf("%d %1f", &poly[i].exp, &poly[i].cof);// 第一个多项式的指数和系数 
        }
        scanf("%d", &m);// 第二个多项式中非零系数的项数
        for(int i = 0; i < m; i++){
            int exp;
            double cof;
            scanf("%d %1f", &exp, &cof);// 第二个多项式的指数和系数 
            for(int j = 0; j < n; j++){
                ans[exp + poly[j].exp] += (cof*poly[j].cof);
            }
        } 
        for(int i = 0; i <= 2000; i++){
            if(ans[i] != 0.0){
                number++;// 累计非零系数的项数 
            }
        }
        printf("%d", number);
        // 从高次幂到低次幂输出 
        for(int i = 2000; i >= 0; i--) {
            if(ans[i] != 0.0]){
                printf(" %d %.1f", i, ans[i]);
        }
        return 0;    
    }

    题解关键

    • 指数相加,系数相乘
  • 相关阅读:
    C++解析一些我们需要的数据
    C语言0数组、柔性数组使用介绍
    正点原子T100智能焊台-试用-拆机测评
    简单工厂模式、工厂模式、抽象工厂模式比较
    一次小模块的使用过程-LC12S无线模块介绍
    正点原子DS100拆解全过程-硬件工程师必备
    STM32通过rosserial接入ROS通讯开发
    Linux下实现Firewalld Net 外网端口转发至内网
    Windows中使用netsh portproxy端口转发
    低延迟网穿透工具FRP
  • 原文地址:https://www.cnblogs.com/YC-L/p/12187393.html
Copyright © 2011-2022 走看看