美文网首页PAT (Advanced Level) Practice
1009 Product of Polynomials (25p

1009 Product of Polynomials (25p

作者: iphelf | 来源:发表于2020-02-28 00:41 被阅读0次

    模拟。
    关键符号说明:

    Symbol Explanation
    nA/nB Number of terms in A/B
    factor Coefficient of the term with certain exponent; For storing A
    base Exponent of a term present in A
    sum Coefficient of the term with certain exponent; For storing product of polynomials
    tB Temporary variable for holding the exponent of a term
    tS Temporary variable for holding the coefficient of a term
    #include<cstdio>
    #include<cstring>
    using namespace std;
    
    const int MAXN=2000;
    int nA,nB;
    double factor[MAXN+1];
    int base[MAXN+1];
    double sum[MAXN+1];
    
    int main(void) {
    //    freopen("in.txt","r",stdin);
        scanf("%d",&nA);
        int tB;
        double tS;
        for(int i=0;i<nA;i++){
            scanf("%d%lf",&tB,&tS);
            base[i]=tB;
            factor[tB]=tS;
        }
        memset(sum,0,sizeof sum);
        scanf("%d",&nB);
        for(int i=0;i<nB;i++){
            scanf("%d%lf",&tB,&tS);
            for(int j=0;j<nA;j++)
                sum[tB+base[j]]+=tS*factor[base[j]];
        }
        int cnt=0;
        for(int i=MAXN;i>=0;i--) if(sum[i]!=0) cnt++;
        printf("%d",cnt);
        for(int i=MAXN;i>=0;i--) if(sum[i]!=0)
            printf(" %d %.1f",i,sum[i]);
        putchar('\n');
        return 0;
    }
    
    /*
    Sample Input:
    2 1 2.4 0 3.2
    2 2 1.5 1 0.5
    
    Sample Output:
    3 3 3.6 2 6.0 1 1.6
    */
    

    相关文章

      网友评论

        本文标题:1009 Product of Polynomials (25p

        本文链接:https://www.haomeiwen.com/subject/qqmqhhtx.html