반응형

문제 출처 :


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



알고리즘 분석 :


문제 해결에 필요한 사항

1. 수학

2. 정렬

3. Map STL :: http://www.crocus.co.kr/604


산술평균, 중앙값, 최빈값, 범위를 구해야 하는 문제이다.


1. 산술평균은 알다시피 수들의 합 / n을 하여 반올림 round함수를 써주면 된다.


2. 중앙값은 sort를 통해 n/2 인덱스의 값을 출력하면 된다. 이때 인덱스가 0번부터 시작이기에 따로 고려할 사항이 없다.


3. 최빈값을 map을 이용하여 어느수가 가장 많이 나왔는지 알아보면 된다.


이때 for문을 한번 더 돌리는데 이 내용은 같은 최빈값이 있을 때 두번째로 작은 수를 나타내기 위해 이용한다.


4. 범위또한 입력받음과 동시에 최대, 최소를 구해두고 MAX - MIN을 해주면 범위를 알 수 있다.













소스 코드 : 


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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <map>
#include <vector>
#include <cmath>
 
using namespace std;
 
int main()
{
    int n;
    scanf("%d"&n);
 
    int sum = 0;
    int MIN = 987654321;
    int MAX = -987654321;
    vector<int> vc;
    map<intint> mp;
    for (int i = 0; i < n; i++)
    {
        int val;
        scanf("%d"&val);
 
        // 범위를 구하기 위해 저장
        MIN = min(MIN, val);
        MAX = max(MAX, val);
 
        // 산술평균을 구하기 위해 저장
        sum += val;
        vc.push_back(val);
        
        // 최빈값을 구하기 위해 저장
        mp[val] ++;
    }
 
 
    // 산술평균 구하기
    cout << (int)round((double)sum / n) << endl;
    
    // 중앙 값 구하기
    sort(vc.begin(), vc.end());
    cout << vc[n / 2<< endl;
 
    // 최빈값 구하기
    int cnt = 0;
    int get = -987654312;
    int pos = 0;
    
    map<intint>::iterator tmp;
 
    for (auto it = mp.begin(); it != mp.end(); it++)
    {
        if (it->second > get)
        {
            get = it->second;
            pos = it->first;
            tmp = it;
        }
    }
 
    for (tmp; tmp != mp.end(); tmp++)
    {
        if (tmp->first != pos && tmp->second == get)
        {
            pos = tmp->first;
            break;
        }
    }
    cout << pos << endl;
    
    // 범위 구하기
    cout << MAX - MIN << endl;
 
    return 0;
}
 
 
//                                                       This source code Copyright belongs to Crocus
//                                                        If you want to see more? click here >>
Crocus


반응형

'Applied > 알고리즘 문제풀이' 카테고리의 다른 글

[2011번] 암호코드  (2) 2017.08.08
[14653번] 너의이름은  (0) 2017.08.07
[2992번] 크면서 작은 수  (0) 2017.08.05
[14659번] 한조서열정리하고옴ㅋㅋ  (0) 2017.08.04
[2306번] 유전자  (0) 2017.08.03