1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <string>
#include <vector>
 
using namespace std;
 
string solution(int a, int b) {
 
    vector<int> vecMonth;
    vecMonth = {312931303130313130313031};
 
    vector<string> vecWeek;
    vecDay = { "FRI""SAT""SUN""MON""TUE""WED""THU" };
 
    int iDay = -1;
    for(int i = 0 ; i < a ; i++)
    {
        if(i == a -1)
            iDay += b;
        else
            iDay += vecMonth[i];        
    }
 
    return vecWeek[iDay & 7];
}
 

의사코드

1. 배열로 월별 일수와 요일을 입력

2. 요일은 2016년이 금요일이므로 FRI부터 시작

3. 입력된 월의 수(a)만큼 더하고 for문의 마지막은 (b)일을 더한다.

4. 배열은 0부터 시작하므로 Day를 모두 더한 Sum값은 -1부터 시작한다.

5. 주배열로부터 요일을 찾아 반환한다.

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
#include <string>
#include <vector>
#include <algorithm>
 
using namespace std;
 
vector<int> solution(vector<int> array, vector<vector<int>> commands) {
    vector<int> answer;
    
    int ArraySize = array.size();
    int CommandSize = commands.size();
    
    for(int i = 0 ; i < CommandSize ; ++i)
    {
        int MinCommand = commands[i][0-1;
        int MaxCommand = commands[i][1-1;
        int CheckNumb = commands[i][2-1;
        
        if(MinCommand == MaxCommand)
        {
            answer.emplace_back(array[MinCommand]);
            continue;
        }
 
        vector<int> vecTemp;
        
        for(int j = MinCommand ; j <= MaxCommand ; j++)
        {
            vecTemp.emplace_back(array[j]);
        }
 
        sort(vecTemp.begin(), vecTemp.end());
 
        answer.emplace_back(vecTemp[CheckNumb]); 
        
    }
        
    return answer;
}
 

 

1. 배열은 0부터 시작하기떄문에 커맨드 숫자 -1

2. for문 사용 시, Min 값과 Max 값이 같으면 for문이 돌지 않기때문에 예외처리

3. 알고리즘 라이브러리의 Sort() 사용

4. 정답 배열에 집어넣음

프로그래머스 레벨 1 모의고사 문제

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 <string>
#include <vector>
 
using namespace std;
 
vector<int> solution(vector<int> answers) {
    vector<int> answer;
    
    vector<int> p1, p2, p3;
    p1 = {1,2,3,4,5};
    p2 = {2,1,2,3,2,4,2,5};
    p3 = {3,3,1,1,2,2,4,4,5,5};
    
    int iCorrect[3= {0, };
    
    //정답 확인
    for(int i = 0 ; i < answers.size() ; i++)
    {
        if(answers[i] == p1[i % 5])
            iCorrect[0+= 1;
        if(answers[i] == p2[i % 8])
            iCorrect[1+= 1;
        if(answers[i] == p3[i % 10])
            iCorrect[2+= 1;
    }
    
    int maxScore = max(max(iCorrect[0], iCorrect[1]), iCorrect[2]);
    
    //순위 확인
    for(int i = 0 ; i < 3 ; i++)
    {
        if(iCorrect[i] == maxScore)
            answer.emplace_back(i+1);
    }
        
    return answer;
}
 

문제의 포인트를 잘잡자.

 

최고 점수를 낸 1명만 배열에 넣으면되고

동점자가 있는 경우는 123 순서로 나오면된다.

 

'코딩테스트 연습' 카테고리의 다른 글

코딩테스트 - 2016년  (0) 2020.01.07
코딩테스트 - K 번째 수  (0) 2020.01.06
코딩테스트 - 완주하지 못한 선수  (0) 2020.01.03
코딩테스트 - 행렬의 곱셈  (0) 2020.01.03
코딩테스트 - 전파탑 문제  (0) 2020.01.02

+ Recent posts