프로그래머스 레벨 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
#include <string>
#include <vector>
 
using namespace std;
 
int solution(int num) {
    int answer = 0;
    
    int iCount = 0;
    long long Number = num;
    
    while(true)
    {
        if(Number == 1)
            return iCount;
        
        if(Number % 2 == 0)
            Number *= 0.5;
        else
        {
            Number *= 3;
            ++Number;
        }
        ++iCount;
        
        if(iCount > 500)
            return -1;
    }
    
    return answer;
}

최초 답안

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int CollatzFunc(long long iNumb)
{
    int iCount = 0;
 
    while (true)
    {
if (iNumb == 1)
return iCount;
 
        (iNumb % 2 == 0) ? iNumb *= 0.5 : (iNumb *= 3+= 1;
        ++iCount;
 
        if (iNumb == 1)
            return iCount;
    }
}
 

함수화하여 깔끔하게 정리한 것.

1-2. 규칙에 의해 int변수인경우 오버플로우가 나므로 long long으로 선언

프로그래머스 레벨 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
#include <string>
#include <vector>
#include <algorithm>
 
using namespace std;
 
class CMath
{
    public:
    int GCDFunc(int a, int b) //최대공약수
    {
        int Min = (a > b) ? a : b;
        int Answer = 1;
        for(int i = Answer ; i <= a ; i++)
        {
            if(a % i == 0 && b % i == 0)
                Answer = i;
        }
        return Answer;
    }
    int LCMFunc(int a, int b)  //최소공배수
    {
        return a * b / GCDFunc(a,b);
    }
};
 
vector<int> solution(int n, int m) {
    vector<int> answer;
    
    CMath c;
    answer.emplace_back(c.GCDFunc(n,m));
    answer.emplace_back(c.LCMFunc(n,m));
    
    return answer;
}
 

최소공배수: 공배수 중에서 가장 작은 수
   (最小公培數, [LCM] Least Common Multiple)

최대공약수: 공약수 중에서 가장 큰 수
   (最大公約數, [GCD] Greatest Common Divisor)

 

 

프로그래머스 레벨 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
#include <vector>
#include <iostream>
 
using namespace std;
 
vector<int> solution(vector<int> arr) 
{
    vector<int> answer;
    
    int PrevNum = -1;
    for(int i = 0 ; i < arr.size() ; i++)
    {
        if(PrevNum == -1)
        {
            answer.emplace_back(arr[i]);
            PrevNum = arr[i];
        }   
        else
        {
            if(PrevNum != arr[i])
            {
                answer.emplace_back(arr[i]);
                PrevNum = arr[i];
            }
        }
    }
 
    return answer;
}
 

내가 만든 코드

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
 
vector<int> solution(vector<int> arr) 
{
 
    arr.erase(unique(arr.begin(), arr.end()),arr.end());
 
    vector<int> answer = arr;
    return answer;
}
 
 

모범 코드

alogorithm 라이브러리의 unique() 사용

unique() 함수 사용

-> 배열에서 연속으로 중복되는 내용을 쓰레기값으로 만든 후 배열의 맨끝으로 보내버리는 함수

unique() 함수 작동완료 시 반환값은 위치값으로, 맨 뒤로 보내진 쓰래기 배열의 첫번째로 이동한다.

레벨 1 테스트

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <string>
#include <vector>
 
using namespace std;
 
string solution(string s) {
    string answer = "";
    
    int strSize = s.length();
    
    if(strSize % 2 == 0// 짝수
    {
        answer += s.at((strSize / 2-1);
        answer += s.at(strSize / 2);
    }
    else
        answer = s.at(strSize / 2);
        
    
    
    return answer;
}
 

내가 푼것.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <string>
#include <vector>
 
using namespace std;
 
string solution(string s) 
{
   int strSize = s.length();
    
    if(strSize % 2 == 0// 짝수
        return s.substr((strSize * 0.5)-1 , 2); 
    else
        return s.substr(strSize * 0.5);
 
    return string();
}
 

모범 답안

string class의 substr을 사용한다.

substr(출력하고싶은 문자열 시작위치, 출력갯수);

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

프로그래머스 레벨 1 완주하지 못한 선수

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
string solution(vector<string> participant, vector<string> completion) 
{
    int iSize = participant.size();
    sort(participant.begin(), participant.end());
    sort(completion.begin(), completion.end());
 
    for(int i = 0 ; i < iSize ; i++)
    {
        //if(participant[i] != completion[i]) //정석 풀이
            //return participant[i];
        if(participant[i] == completion[i]) //내 방식 풀이
            continue;
        else
            return participant[i];
    }
 
    return string();
}
 

algorithm 헤더를 사용해 벡터를 빠르게 정렬할 수 있음.

 

같은 string 비교는 굳이 compare가 아니라 연산자 오버로딩된 것으로도 가능함

 

 

프로그래머스 레벨 2 테스트 문제

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
vector<vector<int>> solution(vector<vector<int>> arr1, vector<vector<int>> arr2) 
{
 
    int arr1Row = arr1.size();
    int arr1Col = arr1[0].size();
    int arr2Row = arr2.size();
    int arr2Col = arr2[0].size();
 
    vector<vector<int>> answer(arr1Row, vector<int>(arr2Col));
 
    int temp  = 0;
 
       for(int i = 0; i < arr1Row; ++i) //행 이동
    {
        for(int j = 0; j < arr2Col; ++j) //열 이동
        {
            temp = 0;
 
            for(int k = 0; k < arr1Col; ++k) //행렬 1 열 이동, 행렬 2 행 이동 및 연산
            {
                temp += arr1[i][k] * arr2[k][j];
            }
            //행렬의 곱을 정답 행렬에 입력
            answer[i][j] = temp;
        }
    }
 
    return answer;
}
 
 

의사코드

1. 두 행렬의 사이즈를 구한다.

2. 1행렬의 행과 2행렬의 열로 정답 행렬을 Reserve 한다.

3. 3중 For문으로 계산해서 답을 넣는다.

 - 3for문 행렬 1의 열 이동, 행렬 2의 행 이동과 연산을 처리

 - 2for문 행렬 2의 열 이동

 - 1for문 행렬 1의 행 이동

프로그래머스 2레벨

 

평행한 수평선위의 여러개의 전파탑에서 동시에 왼쪽 방향으로 송신할때 수신받는 전파탑 찾기

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    vector<int> heights = { 6,9,5,7,4 };
 
    int iSize = heights.size();
    vector<int> answer;
 
    for (int i = 0; i < iSize - 1++i)
    {
        int iPivot = i + 1;
 
        for (int j = i + 1; j >= 1--j)
        {
            if (answer[iPivot] == 0 && heights[iPivot] < heights[j - 1])
            {
                answer[iPivot] = j;
                break;
            }
 
        }
    }
 

 

+ Recent posts