Ex) Level0 - 특이한 정렬
sort() 함수에 익명함수를 넣어서 해결해보았다.
굉장히 신박했음. 익명함수로 Compare()를 만들 생각을 하자.
<hide/>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> solution(vector<int> numlist, int n) {
vector<int> answer;
sort(numlist.begin(), numlist.end(), [=](int a, int b){
if (abs(a - n) == abs(b - n))
{
return a > b;
}
return abs(a - n) < abs(b - n);
});
answer = numlist;
return answer;
}
Ex) Level0 - 저주의 숫자 3[X]
<hide/>
#include <string>
#include <vector>
using namespace std;
bool IsHaving3(int _iN)
{
bool bFlag = false;
while (_iN <= 0)
{
if (3 == _iN % 10 || 0 == ((_iN % 10) % 3))
{
bFlag = true;
break;
}
_iN /= 10;
}
return bFlag;
}
bool IsMultiple(int _iN)
{
bool bFlag = false;
if (_iN % 3 == 0)
{
bFlag = true;
}
return bFlag;
}
int solution(int n) {
int answer = 0;
vector<int> viTable = { 0, 1, 2, 4, 5, 7, 8, 10, 11, 14, 16 };
if (1 <= n && n <= 10)
{
return viTable[n];
}
for (int i = 11; i <= n; ++i)
{
int iTarget = viTable[i - 1] + 1;
while (true)
{
if (true == IsHaving3(iTarget))
{
++iTarget;
}
else if (true == IsMultiple(iTarget))
{
++iTarget;
}
else
{
break;
}
}
viTable.push_back(iTarget);
}
answer = viTable[n];
return answer;
}
Ex) Level0 - 다항식 더하기 [X]
stoi() 함수는 string 헤더에 있음.
substr() 함수의 사용법은 알아두자.
<hide/>
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
string solution(string polynomial) {
string answer = "";
stringstream ss;
ss.str(polynomial);
int iSumOfX = 0;
int iSum = 0;
size_t uSizeOfInput;
while (false == ss.eof())
{
string strInput;
ss >> strInput;
if ("+" == strInput)
{
continue;
}
uSizeOfInput = strInput.size();
if ('x' == strInput[uSizeOfInput - 1])
{
if (1 == uSizeOfInput)
{
++iSumOfX;
}
else
{
string temp = strInput.substr(0, uSizeOfInput - 1);
iSumOfX += stoi(temp);
}
}
else
{
iSum += stoi(strInput);
}
}
if (0 != iSumOfX && 1 != iSumOfX)
{
answer += to_string(iSumOfX) + 'x';
}
if (1 == iSumOfX)
{
answer += 'x';
}
if (0 != iSum)
{
answer += " + " + to_string(iSum);
}
return answer;
}
Ex) Level0 - 최빈값 구하기
오랜만에 보는 최빈값 알고리듬이라 조금 얼탐.
<hide/>
#include <string>
#include <vector>
using namespace std;
#define ELEMENT_SIZE (1001)
int solution(vector<int> array) {
int answer = 0;
size_t uSizeOfArray = array.size();
vector<int> viCount;
viCount.resize(ELEMENT_SIZE, 0);
for (size_t i = 0; i < uSizeOfArray; ++i)
{
++viCount[array[i]];
}
int iMax = 0;
for (size_t i = 0; i < ELEMENT_SIZE; ++i)
{
if (iMax < viCount[i])
{
iMax = viCount[i];
answer = i;
}
}
for (size_t i = 0; i < ELEMENT_SIZE; ++i)
{
if (iMax == viCount[i] && answer != i)
{
return -1;
}
}
return answer;
}
Ex) Level0 - OX퀴즈
<hide/>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
vector<string> solution(vector<string> quiz) {
vector<string> answer;
size_t uSizeOfQuiz = quiz.size();
answer.resize(uSizeOfQuiz, "");
for (size_t i = 0; i < uSizeOfQuiz; ++i)
{
stringstream ss;
ss.str(quiz[i]);
int iRes;
string Operator;
string strInput;
ss >> strInput;
iRes = stoi(strInput);
while (false == ss.eof())
{
ss >> strInput;
if ("=" == strInput)
{
ss >> strInput;
int iCheckRes = stoi(strInput);
if (iCheckRes == iRes)
{
answer[i] += "O";
}
else
{
answer[i] += "X";
}
}
else
{
if ("-" == strInput || "+" == strInput)
{
Operator = strInput;
}
else
{
if ("-" == Operator)
{
iRes -= stoi(strInput);
}
else
{
iRes += stoi(strInput);
}
}
}
}
}
return answer;
}
Ex) Level0 - 다음에 올 숫자 [X]
<hide/>
#include <string>
#include <vector>
using namespace std;
int solution(vector<int> common) {
int answer = 0;
int iCommonRatio;
int iCommonDifference;
bool bFlagForRatio = true;
bool bFlagForDifference = true;
size_t uSizeOfCommon = common.size();
// Initial value.
if (0 == common[0])
{
iCommonRatio = common[2] / common[1];
}
else
{
iCommonRatio = common[1] / common[0];
}
iCommonDifference = common[1] - common[0];
for (size_t i = 2; i < uSizeOfCommon; ++i)
{
if (0 != common[i - 1] && iCommonRatio != (common[i] / common[i - 1]))
{
bFlagForRatio = false;
}
if (iCommonDifference != (common[i] - common[i - 1]))
{
bFlagForDifference = false;
}
}
if (true == bFlagForRatio)
{
answer = common[uSizeOfCommon - 1] * iCommonRatio;
}
else
{
answer = common[uSizeOfCommon - 1] + iCommonDifference;
}
return answer;
}
Ex) Level0 - 분수의 덧셈
<hide/>
#include <string>
#include <vector>
using namespace std;
int CalculateGCD(int _iX, int _iY)
{
int iRemain;
while (0 != _iY)
{
iRemain = _iX % _iY;
_iX = _iY;
_iY = iRemain;
}
return _iX;
}
vector<int> solution(int numer1, int denom1, int numer2, int denom2) {
vector<int> answer;
int iNumerOfRes = numer1 * denom2 + numer2 * denom1;
int iDenomOfRes = denom1 * denom2;
int iGCD = CalculateGCD(iNumerOfRes, iDenomOfRes);
answer.push_back(iNumerOfRes / iGCD);
answer.push_back(iDenomOfRes / iGCD);
return answer;
}
Ex) Level0 - 연속된 수의 합
<hide/>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> solution(int num, int total) {
vector<int> answer;
for (int i = -100; i <= 100 ; ++i)
{
int iSum = 0;
for (int j = 0; j < num; ++j)
{
answer.push_back(i + j);
iSum += (i + j);
}
if (iSum == total)
{
break;
}
else
{
answer.clear();
}
}
sort(answer.begin(), answer.end());
return answer;
}
Ex) Level0 - 안전지대
<hide/>
#include <string>
#include <vector>
using namespace std;
int solution(vector<vector<int>> board) {
int answer = 0;
int iSizeOfBoard = static_cast<int>(board.size());
for (int i = 0; i < iSizeOfBoard; ++i)
{
for (int j = 0; j < iSizeOfBoard; ++j)
{
bool bSafe = true;
for (int k = -1; k <= 1; ++k)
{
for (int l = -1; l <= 1; ++l)
{
int iRow = i + k;
if (iRow < 0 || iSizeOfBoard <= iRow)
{
continue;
}
int iCol = j + l;
if (iCol < 0 || iSizeOfBoard <= iCol)
{
continue;
}
if (1 == board[iRow][iCol])
{
bSafe = false;
break;
}
}
if (false == bSafe)
{
break;
}
}
if (true == bSafe)
{
++answer;
}
}
}
return answer;
}
Ex) Level0 - 옹알이 (1)
<hide/>
#include <string>
#include <vector>
using namespace std;
void Permutation(int _iN, int _iCurrentR, int _iR, vector<string>& _vstrInput, vector<bool> _vbVisit, string& _strResult, string& _strBabbling, int& _iAnswer)
{
if (_iCurrentR == _iR)
{
if (_strResult == _strBabbling)
{
++_iAnswer;
}
return;
}
else
{
for (int i = 0; i < _iN; ++i)
{
if (true == _vbVisit[i])
{
continue;
}
_vbVisit[i] = true;
string temp = _strResult + _vstrInput[i];
Permutation(_iN, _iCurrentR + 1, _iR, _vstrInput, _vbVisit, temp, _strBabbling, _iAnswer);
_vbVisit[i] = false;
}
}
}
int solution(vector<string> babbling) {
int answer = 0;
vector<string> vstrInput = { "aya", "ye", "woo", "ma" };
vector<bool> vbVisit;
vbVisit.resize(5, false);
size_t uSizeOfBabbling = babbling.size();
string strResult = "";
for (size_t i = 0; i < uSizeOfBabbling; ++i)
{
string strBabbling = babbling[i];
for (size_t j = 1; j <= 4; ++j)
{
Permutation(4, 0, j, vstrInput, vbVisit, strResult, strBabbling, answer);
}
}
return answer;
}
댓글