1. 程式人生 > >PAT 甲級測試題目 -- 1009 Product of Polynomials

PAT 甲級測試題目 -- 1009 Product of Polynomials

href 做了 () pin .cn ets sco double str

題目鏈接
這題占個坑吧。。。我用了兩種思路,一種將保存結果的數組初始化為 0,把乘積加上去,順便記錄下最後一個指數的值。用兩次 2000 次 for 循環過濾掉 0 值以及輸出答案,這個代碼過了,但是後期測試的時候,有些測試用例會多出來空格,有些則格式不對。。但是過了。。。
代碼如下

#include<iostream>
#include<stdio.h>
using namespace std;
int main() {
    double A[1001], B[1001];
    int ExpA[11], ExpB[11];
    double Result[2001];
    double coe;
    int Acounts, Bcounts, exp, EndIndex = 0;

    // 初始化多項式


    for (int i = 0; i < 1001; i++) {
        A[i] = B[i] = 0;
    }
    for (int i = 0; i < 2001; i++) {
        Result[i] = 0;
    }

    cin >> Acounts;
    for (int i = 0; i < Acounts; i++) {
        cin >> exp >> coe;
        ExpA[i] = exp;
        A[exp] = coe;
    }
    cin >> Bcounts;
    for (int i = 0; i < Bcounts; i++) {
        cin >> exp >> coe;
        ExpB[i] = exp;
        B[exp] = coe;
    }

    for (int i = 0; i < Acounts; i++) {
        for (int j = 0; j < Bcounts; j++) {
            //cout << "=====================" << endl;
            //cout << "Result[ExpA[i] + ExpB[j]] = " << Result[ExpA[i] + ExpB[j]] << endl;
            Result[ExpA[i] + ExpB[j]] += A[ExpA[i]] * B[ExpB[j]];
            //cout << "A[ExpA[i]] * B[ExpB[j]] = " << A[ExpA[i]] * B[ExpB[j]] << endl;
            //cout << "Result[ExpA[i] + ExpB[j]] = " << Result[ExpA[i] + ExpB[j]] << endl;
            //cout << "=====================" << endl;
            EndIndex = ExpA[i] + ExpB[j];
        }
    }

    int AllResCount = 0;

    for (int i = 2000; i >= 0; i--) {
        if (Result[i] != 0) {
            AllResCount++;
        }

    }

    cout << AllResCount << " ";
    for (int i = 2000; i >= 0; i--) {
        if (Result[i] != 0) {
            printf("%d %.1f", i, Result[i]);
            if (i != EndIndex)
                cout << " ";

        }

    }

    return 0;
}

另外一種思路是將保存結果的數組初始化為 -1 ,邊計算多項式邊記錄非零多項式的項數,最後輸出答案,測試點 0 答案錯誤。。。我百度了好久,好多帖子指出是跟系數為 0 有關,我自己做了幾個系數為 0 測試用例,測試結果和我在紙上計算的答案一致,包括格式也正確,我實在弄不懂了。。。代碼如下:

#include<iostream>
#include<stdio.h>
using namespace std;
int main() {
    double A[1001], B[1001];
    int ExpA[11], ExpB[11], ItemIndex[10];
    double Result[2001];
    double coe;
    int Acounts, Bcounts, exp, ResCounts = 0;

    // 初始化多項式


    for (int i = 0; i < 1001; i++) {
        A[i] = B[i] = -1;
    }
    for (int i = 0; i < 2001; i++) {
        Result[i] = -1;
    }

    cin >> Acounts;
    for (int i = 0; i < Acounts; i++) {
        cin >> exp >> coe;
        ExpA[i] = exp;
        A[exp] = coe;
    }
    cin >> Bcounts;
    for (int i = 0; i < Bcounts; i++) {
        cin >> exp >> coe;
        ExpB[i] = exp;
        B[exp] = coe;
    }

    int indexCount = 0;
    for (int i = 0; i < Acounts; i++) {     
        for (int j = 0; j < Bcounts; j++) {         
            if (Result[ExpA[i] + ExpB[j]] == -1) {
                Result[ExpA[i] + ExpB[j]] = A[ExpA[i]] * B[ExpB[j]];

                if (Result[ExpA[i] + ExpB[j]] != 0) {
                    ResCounts++;
                }
            }
            else {
                if (Result[ExpA[i] + ExpB[j]] == 0 && A[ExpA[i]] * B[ExpB[j]] != 0)
                    ResCounts++;
                Result[ExpA[i] + ExpB[j]] += A[ExpA[i]] * B[ExpB[j]];
            }
            
        }
    }

    cout << ResCounts << " ";
    for (int i = 2000; i >= 0; i--) {
        
        if (Result[i] != -1 && Result[i] != 0) {
            ResCounts--;
            //cout << "----" << ResCounts << "----" << endl;
            printf("%d %.1f", i, Result[i]);
            if (ResCounts != 0)
                printf(" ");
        }
    }

    return 0;
}

希望有大佬可以指出錯誤QAQ

PAT 甲級測試題目 -- 1009 Product of Polynomials