PAT (Advanced Level) Practice

1009 Product of Polynomials (25p

2020-02-28  本文已影响0人  iphelf

模拟。
关键符号说明:

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
*/
上一篇 下一篇

猜你喜欢

热点阅读