Ex) Level1 - 3진법 뒤집기
<hide/>
#include <string>
#include <vector>
using namespace std;
int solution(int n) {
int answer = 0;
string strTrinary = "";
while (0 < n)
{
strTrinary += to_string(n % 3);
n /= 3;
}
size_t uSizeOfTrinary = strTrinary.size();
for (size_t i = 0; i < uSizeOfTrinary; ++i)
{
answer = answer * 3 + (strTrinary[i] - '0');
}
return answer;
}
Ex) Level1 - 이상한 문자 만들기 [X]
<hide/>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
string solution(string s) {
string answer = "";
stringstream ss;
ss.str(s);
string strWord = "";
size_t uSizeOfWord;
while (false == ss.eof())
{
ss >> strWord;
uSizeOfWord = strWord.size();
for (size_t i = 0; i < uSizeOfWord; ++i)
{
if (i % 2 == 0)
{
answer += toupper(strWord[i]);
}
else
{
answer += tolower(strWord[i]);
}
}
if (false == ss.eof())
{
answer += ' ';
}
}
return answer;
}
Ex) Level1 - 예산 [X]
탐욕법? 인듯한데..
<hide/>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int solution(vector<int> d, int budget) {
int answer = 0;
sort(d.begin(), d.end(), [](int a, int b) {
return a > b;
});
int iSizeOfD = static_cast<int>(d.size());
int iSum = 0;
int i;
for (i = 0; i < iSizeOfD; ++i)
{
iSum += d[i];
answer = i + 1;
if (budget < iSum)
{
break;
}
}
return answer;
}
댓글