[백준/C++] 5596 - 시험 점수

orangesnail·2025년 5월 7일

백준

목록 보기
102/169

https://www.acmicpc.net/problem/5596


초기 코드

처음에는 그냥 단순무식하게.. 다 입력받고 합 계산하고 max 출력하는 식으로 짰다.

#include <iostream>
using namespace std;

int main() {
    int m1, m2, m3, m4, n1, n2, n3, n4;
    int total1 = 0, total2 = 0;

    cin >> m1 >> m2 >> m3 >> m4;
    cin >> n1 >> n2 >> n3 >> n4;

    total1 = m1 + m2 + m3 + m4;
    total2 = n1 + n2 + n3 + n4;

    cout << max(total1, total2);

    return 0;
}

개선된 코드

배열과 for문을 사용하면 위의 코드에서 입력 부분의 중복을 없앨 수 있다!

#include <iostream>
using namespace std;

int main() {
    int score[2][4];
    int total[2] = {0, 0};

    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 4; j++) {
            cin >> score[i][j];
            total[i] += score[i][j];
        }
    }

    cout << max(total[0], total[1]) << endl;

    return 0;
}
profile
초보입니다. 피드백 환영합니다 😗

0개의 댓글