zoukankan      html  css  js  c++  java
  • PAT甲题题解-1009. Product of Polynomials (25)-多项式相乘

    多项式相乘

    注意相乘结果的多项式要开两倍的大小!!!

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #include <string.h>
    using namespace std;
    //两个多项式相乘
    const int maxn=1000+5;
    int k;
    //原本定义个exp导致编译错误,没想到定义成exp1、exp2也会编译错误,估计引起函数名冲突了
    double exps1[maxn];
    double exps2[maxn];
    int main()
    {
        memset(exps1,0,sizeof(exps1));
        memset(exps2,0,sizeof(exps2));
        int ee;
        double val;
        scanf("%d",&k);
        for(int i=0;i<k;i++){
            scanf("%d %lf",&ee,&val);
            exps1[ee]=val;
        }
        scanf("%d",&k);
        for(int i=0;i<k;i++){
            scanf("%d %lf",&ee,&val);
            exps2[ee]=val;
        }
        double ans[maxn*2]; //注意这里要开2倍的大小
        memset(ans,0,sizeof(ans));
        for(int i=0;i<maxn;i++){
            for(int j=0;j<maxn;j++){
                ans[i+j]+=exps1[i]*exps2[j];
            }
        }
        int cnt=0;
        for(int i=0;i<maxn*2;i++){
            if(ans[i]!=0)
                cnt++;
        }
        printf("%d",cnt);
        for(int i=maxn*2-1;i>=0;i--){
            if(ans[i]!=0){
                printf(" %d %.1lf",i,ans[i]);
            }
        }
        return 0;
    }
    View Code
  • 相关阅读:
    vue的单向数据流
    vue的组件基础
    vue v-for的数组改变导致页面不渲染解决方法
    Binary Heap
    Types of Binary Tree
    Merge Sort
    Master Theorem
    Insertion Sort
    Amazon Redshift and Massively Parellel Processing
    Bubble Sort
  • 原文地址:https://www.cnblogs.com/chenxiwenruo/p/6727906.html
Copyright © 2011-2022 走看看