[C++] 백준 2592 : 대표값

Kim Nahyeong·2022년 2월 10일
0

백준

목록 보기
85/157

  #include <iostream>
  using namespace std;

  int most, mostCnt = 0, x;
  int sum = 0;
  int cnt[101] = {0}; // 자연수는 1000보다 작은 10의 배수
  int main(int argc, char** argv){
    for(int i = 0; i < 10; i++){
      scanf("%d", &x);
      sum += x;
      cnt[x / 10]++;
      if(cnt[x / 10] > mostCnt){
        mostCnt = cnt[x/10]; // most는 횟수 세기
        most = x / 10;
      }
    }

    printf("%d\n", sum / 10);
    printf("%d\n", most * 10);

    return 0;
  }

쉬운데 5번이나 틀린 문제. 오늘은 날이 아닌가보다. 최빈값 자체를 재는 것과 실제 최빈값(index)는 다르다는 것을 명심하자.

0개의 댓글