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;    
}
复制代码

题解关键#

  • 指数相加,系数相乘
posted @   BigBender  阅读(244)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示
主题色彩