天天看點

PAT甲級1009 Product of Polynomials (25分)

原題目:

PAT甲級1009 Product of Polynomials (25分)

源代碼:

#include<iostream>
#include<vector>
#include<stdio.h>
#include<cmath>
using namespace std;
struct Ploy{
    int n;
    float a;
};
float res[1000005];
vector<Ploy> x, y;
int main(){
    int k1, k2;
    cin>>k1;
    for(int j = 0; j < k1; j ++){
        Ploy tmp;
        cin>>tmp.n>>tmp.a;
        x.push_back(tmp);
    }
    cin>>k2;
    for(int j = 0; j < k2; j ++){
        Ploy tmp;
        cin>>tmp.n>>tmp.a;
        y.push_back(tmp);
    }
    for(int i = 0; i < k1; i ++){
        int e = x[i].n;
        float f = x[i].a;
        for(int j = 0; j < k2; j ++){
            res[y[j].n + e] += (f * y[j].a);
        }
    }
    int k_res = 0, max_i;
    for(int i = 0; i < 1000005; i ++){
        if(res[i] != 0){
            max_i = i;
            k_res ++;
        }
    }
    cout<<k_res<<" ";
    int flag = 0;
    for(int i = max_i; i >= 0; i --){
        if(res[i] != 0){
            cout<<i<<" ";
            printf("%.1f", res[i]);
            flag ++;
            if(flag != k_res)
                cout<<" ";
        }
    }
    return 0;
}
           

已AC:

PAT甲級1009 Product of Polynomials (25分)

易失分點:

規模較小,直接用數組存結果沒問題

如果用連結清單存結果,記得删掉值為0的節點

(題目樣例比較簡單,其實系數為浮點型時無法用是否等于0判斷)