티스토리 뷰

반응형


#. Problem

* The copyright in this matter is in Inflearn


#. Resolution Process

  1. Read and understand problem

  2. Redefine the problem + abstract

  3. Create solution plan (select Algorithm, Data structure)

  4. Prove the plan (check performance time and usage memory)

  5. Carry out the plan

  6. Look back on the plan and find a way to improve it


#. Solve

  1. 선택정렬을 응용해서 풀라고 했으니, 선택정렬을 사용해서 풀어보자.

     

     80 96 75 82 96 92 100 에서 3등의 성적을 구해야 한다.

     당연 정렬을 먼저 해주어야겠다.

     단, 이전 문제와 다른 점은 내림차순으로 정렬을 해야 한다는 것!


     100 96 96 92 82 80 75 

     여기서 96 두 명은 모두 2등으로 간주하므로, 3등의 성적은 92가 될 것이다.

     그러므로, 첫 번째 친구는 무조건 1등이 도리 것이므로 두 번째 친구부터 i - 1번째 친구와 비교해서 점수가 같다면 넘어가고

     점수가 다르다면 rank++ 를 해주면 된다. 여기서 rank가 3이 되었을 때의 성적을 출력해주면 된다.


#. Code

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
#include <cstdio>
#include <vector>
#include <algorithm>
 
using namespace std;
 
int main(void)
{
    freopen("input.txt""rt", stdin);
 
    int n, i, j, idx, tmp, rank = 1;
 
    scanf("%d"&n);
    vector<int> v(n);
 
    for (i = 0; i < n; i++)
        scanf("%d"&v[i]);
 
    for (i = 0; i < n - 1; i++)
    {
        idx = i;
 
        for (j = i + 1; j < n; j++)
        {
            if (v[j] > v[idx])
                idx = j;
        }
 
        tmp = v[i];
        v[i] = v[idx];
        v[idx] = tmp;
    }
 
    for (i = 1; i < n; i++)
    {
        if (v[i] != v[i - 1])
            rank++;
 
        if (rank == 3)
        {
            printf("%d\n", v[i]);
            break;
        }
    }
 
    return 0;
}
cs


#. Other code

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
#include<stdio.h>
 
int main() {
    int a[101], n, tmp, idx, i, j, cnt=0;
 
    scanf("%d"&n);
 
    for(i=0; i<n; i++)
        scanf("%d"&a[i]);
    
    for(i=0; i<n-1; i++){
        idx=i;
 
        for(j=i+1; j<n; j++){
            if(a[j]>a[idx]) 
                idx=j;
        }
 
        tmp=a[i];
        a[i]=a[idx];
        a[idx]=tmp;
    }
 
    for(i=1; i<n; i++){
        if(a[i-1]!=a[i]) 
            cnt++;
 
        if(cnt==2){
            printf("%d\n", a[i]);
            break;
        }
    }
 
    return 0;
}
cs

사실 이 문제의 핵심은 정렬이 아니라 3등의 성적을 찾는 것인데,

강사님이 짜신 코드와 유사하게 잘 짠것 같당


#. Result

  - Input --------------------------------------------------------

7

80 96 75 82 96 92 100

------------------------------------------------------------------


  - Output --------------------------------------------------------

92

------------------------------------------------------------------



반응형
댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday