본문 바로가기
C++/프로그래머스 문제풀이

23-01-11

by GameStudy 2023. 1. 11.

Ex) Level0 - 7의 개수

<hide/>

#include <string>
#include <vector>

using namespace std;

void Count(int _iN, int& _refAnswer)
{
    if (_iN <= 0)
    {
        return;
    }
    
    Count(_iN / 10, _refAnswer);
    
    if (_iN % 10 == 7)
    {
        ++_refAnswer;
    }
    
}

int solution(vector<int> array) {
    int answer = 0;
    
    size_t uSizeOfArray = array.size();
    for (size_t i = 0; i < uSizeOfArray; ++i)
    {
        Count(array[i], answer);
    }
    
    return answer;
}

 

Ex) Level0 - 이진수 더하기

  또 반복문에서 size_t와 --를 함께 썼음... 주의하자.

  반복 풀이가 필요할듯. 굉장히 오래 걸림.

<hide/>

#include <string>
#include <vector>
#include <algorithm>

using namespace std;

#define STACK_SIZE (16)
#define NOTATION (2)

class Stack
{
public:
    Stack()
        : m_data{ 0, }
        , m_uTop(0u)
    {
    }

    void Push(char _chData)
    {
        if (STACK_SIZE - 1 <= m_uTop)
        {
            // Overflow.
            return;
        }

        m_data[++m_uTop] = _chData;
    }

    bool Empty()
    {
        return 0u == m_uTop;
    }

    int Pop()
    {
        if (true == Empty())
        {
            // Underflow.
            return 0;
        }

        return m_data[m_uTop--];
    }

public:
    int m_data[STACK_SIZE];
    size_t m_uTop;

};

string solution(string bin1, string bin2) {
    string answer = "";

    size_t uSizeOfBin1 = bin1.size();
    size_t uSizeOfBin2 = bin2.size();
    size_t uMaxSize = max(uSizeOfBin1, uSizeOfBin2);

    Stack sBin1;
    for (size_t i = 0; i < uSizeOfBin1; ++i)
    {
        sBin1.Push(bin1.at(i) - '0');
    }
    Stack sBin2;
    for (size_t i = 0; i < uSizeOfBin2; ++i)
    {
        sBin2.Push(bin2.at(i) - '0');
    }

    int iSum = 0;
    int iCarry = 0;
    for (size_t i = 0; i < uMaxSize; ++i)
    {
        iSum = iCarry + sBin1.Pop() + sBin2.Pop();
        if (NOTATION <= iSum)
        {
            answer += (iSum % NOTATION) + '0';
            iCarry = 1;
        }
        else
        {
            answer += (iSum % NOTATION) + '0';
            iCarry = 0;
        }
    }
    
    // Calculate MSB.
    iSum = iCarry + sBin1.Pop() + sBin2.Pop();
    if (0 != iSum)
    {
        if (NOTATION <= iSum)
        {
            answer += (iSum % NOTATION) + '0';
            iCarry = 1;
        }
        else
        {
            answer += (iSum % NOTATION) + '0';
            iCarry = 0;
        }
    }

    reverse(answer.begin(), answer.end());

    return answer;
}

 

Ex) Level0 - 숨어있는 숫자의 덧셈 (2)

<hide/>

#include <string>
#include <vector>

using namespace std;

int solution(string my_string) {
    int answer = 0;
    
    
    size_t uSizeOfMyString = my_string.size();
    int iSumOfUnit = 0;
    for (size_t i = 0; i < uSizeOfMyString; ++i)
    {
        if ('0' <= my_string.at(i) && my_string.at(i) <= '9')
        {
            iSumOfUnit = iSumOfUnit * 10 + (my_string.at(i) - '0');
        }
        else
        {
            answer += iSumOfUnit;
            iSumOfUnit = 0;
        }
    }
    
    // Calculate remains.
    answer += iSumOfUnit;
    
    return answer;
}

 

Ex) Level0 - 공 던지기

<hide/>

#include <string>
#include <vector>

using namespace std;

#define OFFSET (2)

int solution(vector<int> numbers, int k) {
    int answer = 0;
    int iSizeOfNumbers = numbers.size();
    int i = 0;
    size_t uCount = 1;
    while (true)
    {
        if (k == uCount)
        {
            answer = numbers.at(i);
            break;
        }
        i = (i + OFFSET) % iSizeOfNumbers;
        ++uCount;
    }
    
    return answer;
}

'C++ > 프로그래머스 문제풀이' 카테고리의 다른 글

23-01-17  (0) 2023.01.17
23-01-16  (0) 2023.01.16
23-01-15  (0) 2023.01.15
23-01-10  (0) 2023.01.10
23-01-09  (0) 2023.01.09

댓글