작성일 :

문제 링크

2822번 - 점수 계산

설명

이 문제는 8개의 점수 중 상위 5개의 점수 합과 해당 점수의 인덱스(1-based)를 구하여 출력하는 문제입니다.

출력은 다음과 같이 구성됩니다:

  • 상위 5개 점수의 총합
  • 해당 점수의 입력 순서 인덱스를 오름차순으로 출력

접근법

  • 점수와 해당 인덱스를 쌍(pair)로 저장한 뒤, 점수를 기준으로 내림차순 정렬합니다.
  • 상위 5개의 점수만 선택하여 합산하고, 해당 인덱스를 배열에 저장합니다.
  • 최종적으로 인덱스를 오름차순으로 정렬하여 출력합니다.

Code

[ C# ]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;
using System.Linq;

namespace Solution {
  class Program {
    static void Main(string[] args) {
      var score = new (int value, int idx)[8];
      for (int i = 0; i < 8; i++)
        score[i] = (int.Parse(Console.ReadLine()!), i + 1);

      var top5 = score.OrderByDescending(s => s.value).Take(5).ToArray();
      var sum = top5.Sum(s => s.value);
      var indices = top5.Select(s => s.idx).OrderBy(x => x);

      Console.WriteLine(sum);
      Console.WriteLine(string.Join(" ", indices));
    }
  }
}



[ C++ ]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <bits/stdc++.h>

using namespace std;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

bool comp(const pii& a, const pii& b) {
  return a.first > b.first;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  vpii score(8);
  for (int i = 0; i < 8; i++) {
    cin >> score[i].first;
    score[i].second = i + 1;
  }

  sort(score.begin(), score.end(), comp);

  int sum = 0, ans[5];
  for (int i = 0; i < 5; i++) {
    ans[i] = score[i].second;
    sum += score[i].first;
  }

  sort(ans, ans + 5);

  cout << sum << "\n";
  for (int i = 0; i < 5; i++)
    cout << ans[i] << " ";
  cout << "\n";

  return 0;
}