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;
}