알고리즘 문제

프로그래머스 - 코딩 기초 트레이닝 캘린더 Day 8

als982001 2024. 2. 17. 13:56

1. 간단한 논리 연산

https://school.programmers.co.kr/learn/courses/30/lessons/181917

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

bool solution(bool x1, bool x2, bool x3, bool x4) {
    bool answer = true;
    
    answer = (x1 || x2) && (x3 || x4);
    
    return answer;
}

 

 문제의 주어진 식, (x1 ∨ x2) ∧ (x3 ∨ x4)에서 ∨가 ||, ∧가 &&이므로 그대로 적용하면 되는 간단한 문제이다.

 


 

2. 주사위 게임 3

https://school.programmers.co.kr/learn/courses/30/lessons/181916

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

#include <string>
#include <vector>
using namespace std;

int Min(int x, int y)
{
    return x < y ? x : y;
}

int Abs(int num)
{
    return num > 0 ? num : -num;
}

// 네 주사위에서 나온 숫자가 모두 p로 같을 경우
int CheckCase1(int a, int b, int c, int d)
{
    if (a == b && b == c && c == d && d == a)
        return a;
    else
        return -1;
}

// 세 주사위에서 나온 숫자가 p로 같고 나머지 다른 주사위에서 나온 숫자가 q일 때
pair<int, int> CheckCase2(int a, int b, int c, int d)
{
    // a, b, c        d
    if (a == b && b == c && c == a)
    {
        if (a != d) 
            return { a, d };
        else
            return { -1, -1 };
    }
    
    // a, b, d      c
    if (a == b && b == d && d == a)
    {
        if (a != c) 
            return { a, c };
        else
            return { -1, -1 };
    }
    
    // a, c, d       b
    if (a == c && c == d && d == a)
    {
        if (a != b) 
            return { a, b };
        else
            return { -1, -1 };
    }
    
    // b, c, d       a
    if (b == c && c == d && d == b)
    {
        if (b != a) 
            return { b, a };
        else
            return { -1, -1 };
    }
    
    return { -1, -1 };
}

// 주사위가 두 개씩 같은 값이 나오고, 나온 숫자를 각각 p, q인지 확인하는 함수
pair<int, int> CheckCase3(int a, int b, int c, int d)
{
    // a, b     c, d
    if (a == b && c == d)
    {
        if (d != a)
            return { a, c };
        else 
            return { -1, -1 };
    }
    
    // a, c     b, d
    if (a == c && b == d)
    {
        if (b != a)
            return { a, b };
        else
            return { -1, -1 };
    }
    
    // a, d     b, c
    if (a == d && b == c)
    {
        if (a != c)
            return { a, b };
        else
            return { -1, -1 };
    }
    
    return { -1, -1 };
}

// 어느 두 주사위에서 나온 숫자가 p로 같고 나머지 두 주사위에서 나온 숫자가 각각 p와 다른 q, r일 때
pair<int, int> CheckCase4(int a, int b, int c, int d)
{
    // a, b     c     d
    if (a == b)
    {
        if (a != c && a != d && c != d)
            return { c, d };
        else
            return { -1, -1 };
    }
    
    // a, c     b     d
    if (a == c)
    {
        if (a != b && a != d && b != d)
            return { b, d };
        else
            return { -1, -1 };
    }
    
    // a, d     b     c
    if (a == d)
    {
        if (a != b && a != c && b != c)
            return { b, c };
        else
            return { -1, -1 };
    }
    
    // b, c     d     a
    if (b == c)
    {
        if (b != d && b != a && d != a)
            return { a, d };
        else
            return { -1, -1 };
    }
    
    // b, d     a     c
    if (b == d)
    {
        if (b != a && b != c && a != c)
            return { a, c };
        else
            return { -1, -1 };
    }
    
    // c, d     a     b
    if (c == d)
    {
        if (c != a && c != b && a != b)
            return { a, b };
        else
            return { -1, -1 };
    }
    
    return { -1, -1 };
}

int solution(int a, int b, int c, int d) {
    
    int case1 = CheckCase1(a, b, c, d);
    
    if (case1 != -1)
        return case1 * 1111;
    
    pair<int, int> case2 = CheckCase2(a, b, c, d);
    
    if (case2.first != -1)
        return (10 * case2.first + case2.second) * (10 * case2.first + case2.second);
    
    pair<int, int> case3 = CheckCase3(a, b, c, d);
    
    if (case3.first != -1)
        return (case3.first + case3.second) * Abs(case3.first - case3.second);
    
    pair<int, int> case4 = CheckCase4(a, b, c, d);
    
    if (case4.first != -1)
        return case4.first * case4.second;
    
    return min(min(min(a, b), c), d);
}

 

 문제에서 주어진 각 case를 일일이 검사하였다.

 


 

3. 글자 이어 붙여 문자열 만들기

https://school.programmers.co.kr/learn/courses/30/lessons/181915

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

#include <string>
#include <vector>

using namespace std;

string solution(string my_string, vector<int> index_list) {
    string answer = "";
    
    for (int i = 0; i < index_list.size(); ++i)
    {
        int index = index_list[i];
        
        answer += my_string[index];
    }
    
    return answer;
}

 

 반복문을 이용해 index_list의 index 위치의 문자를 하나씩 추가해주면 된다.

 


 

4. 9로 나눈 나머지

https://school.programmers.co.kr/learn/courses/30/lessons/181914

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

#include <string>
#include <vector>

using namespace std;

int solution(string number) {
    int answer = 0;
    
    for (int i = 0; i < number.size(); ++i)
    {
        int num = number[i] - '0';
        answer += num;
    }
    
    answer %= 9;
    
    return answer;
}

 

 number가 string 타입이기에 반복문을 이용해 각 자리 숫자를 하나씩 더해주면 된다. 그리고 각 자리 숫자를 모두 더한 값이 9 이상일 수 있기 때문에 마지막에 9로 나눈 나머지를 한 번 더 구한다.

 


 

5. 문자열 여러 번 뒤집기

https://school.programmers.co.kr/learn/courses/30/lessons/181913

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

#include <string>
#include <vector>

using namespace std;

string solution(string my_string, vector<vector<int>> queries) {
    string answer = "";
    
    for (int queryIdx = 0; queryIdx < queries.size(); ++queryIdx)
    {
        int startIdx = queries[queryIdx][0];
        int endIdx = queries[queryIdx][1];
        
        string tempStr = "";
        
        for (int strIdx = endIdx; strIdx >= startIdx; --strIdx)
            tempStr += my_string[strIdx];
        
        for (int strIdx = startIdx, tempStrIdx = 0; strIdx <= endIdx; ++strIdx, ++tempStrIdx)
            my_string[strIdx] = tempStr[tempStrIdx];
    }
    
    answer = my_string;
    
    return answer;
}

 

 문자열을 뒤집기 위해, tempStr에 주어진 쿼리 범위의 문자열을 미리 뒤집은 문자열을 저장하여 이용하였다.