#54023: 解(C++) 我比較不會用vector不然應該可以更簡單


lingolin22@gmail.com (LINLIN)


#include <iostream>
using namespace std;

int main() {
    int N, M;
    cin >> N >> M;

    int maxList[1000];    

for (int i = 0; i < N; i++) {
        int mx = 0;
        for (int j = 0; j < M; j++) {
            int x;
            cin >> x;
            if (x > mx) mx = x;
        }
        maxList[i] = mx;
    }

    int S = 0;
    for (int i = 0; i < N; i++) {
        S += maxList[i];
    }

    cout << S << endl;


    bool found = false;
    for (int i = 0; i < N; i++) {
        if (S % maxList[i] == 0) {
            if (found) cout << " ";
            cout << maxList[i];
            found = true;
        }
    }

    if (!found) cout << -1;

    cout << endl;
    return 0;
}

#54024: Re: 解(C++) 我比較不會用vector不然應該可以更簡單


lingolin22@gmail.com (LINLIN)


#include <iostream>
using namespace std;

int main() {
    int N, M;
    cin >> N >> M;

    int maxList[1000];   // 假設 N 不會超過 1000(APCS 足夠)

    // 讀入每群、找最大值
    for (int i = 0; i < N; i++) {
        int mx = 0;
        for (int j = 0; j < M; j++) {
            int x;
            cin >> x;
            if (x > mx) mx = x;
        }
        maxList[i] = mx;
    }

    // 計算最大和 S
    int S = 0;
    for (int i = 0; i < N; i++) {
        S += maxList[i];
    }

    cout << S << endl;

    // 找出能整除 S 的最大值
    bool found = false;
    for (int i = 0; i < N; i++) {
        if (S % maxList[i] == 0) {
            if (found) cout << " ";
            cout << maxList[i];
            found = true;
        }
    }

    if (!found) cout << -1;

    cout << endl;
    return 0;
}

標註了一下,還有第一版應該有錯?