Ex) Level0 - 문자열 안에 문자열
string::npos와 string::find() 함수 사용법을 몰랐음.
<hide/>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int solution(string str1, string str2) {
int answer = 0;
if (string::npos == str1.find(str2))
{
answer = 2;
}
else
{
answer = 1;
}
return answer;
}
Ex) Level0 - 자릿수 더하기
<hide/>
#include <string>
#include <vector>
using namespace std;
int solution(int n) {
int answer = 0;
if (0 == n)
{
return answer;
}
while (0 < n)
{
answer += n % 10;
n /= 10;
}
return answer;
}
Ex) Level0 - 제곱수 판별하기
<hide/>
#include <string>
#include <vector>
using namespace std;
int solution(int n) {
int answer = 2;
for (int i = 1; i * i <= n; ++i)
{
if (i * i == n)
{
answer = 1;
}
}
return answer;
}
Ex) Level0 - 숨어있는 숫자의 덧셈 (1)
<hide/>
#include <string>
#include <vector>
using namespace std;
int solution(string my_string) {
int answer = 0;
size_t uSizeOfMyString = my_string.size();
for (size_t i = 0; i < uSizeOfMyString; ++i)
{
if ('1' <= my_string.at(i) && my_string.at(i) <= '9')
{
answer += my_string.at(i) - '0';
}
}
return answer;
}
Ex) Level0 - 개미 군단
<hide/>
#include <string>
#include <vector>
using namespace std;
#define GENERAL (5)
#define SOLDIER (3)
#define PAWN (1)
int solution(int hp) {
int answer = 0;
answer += hp / GENERAL;
hp = hp % GENERAL;
answer += hp / SOLDIER;
hp = hp % SOLDIER;
answer += hp / PAWN;
return answer;
}
Ex) Level0 - 모음 제거
<hide/>
#include <string>
#include <vector>
using namespace std;
string solution(string my_string) {
string answer = "";
size_t uSizeOfMyString = my_string.size();
for (size_t i = 0; i < uSizeOfMyString; ++i)
{
if ('a' == my_string.at(i) ||
'e' == my_string.at(i) ||
'i' == my_string.at(i) ||
'o' == my_string.at(i) ||
'u' == my_string.at(i) )
{
continue;
}
answer += my_string.at(i);
}
return answer;
}
Ex) Level0 - n의 배수 고르기
<hide/>
#include <string>
#include <vector>
using namespace std;
vector<int> solution(int n, vector<int> numlist) {
vector<int> answer;
size_t uSizeOfNumList = numlist.size();
for (size_t i = 0; i < uSizeOfNumList; ++i)
{
if (numlist.at(i) % n == 0)
{
answer.push_back(numlist.at(i));
}
}
return answer;
}
Ex) Level0 - 세균 증식
<hide/>
#include <string>
#include <vector>
using namespace std;
int solution(int n, int t) {
int answer = n;
for (int i = 1; i <= t; ++i)
{
answer *= 2;
}
return answer;
}
Ex) Level0 - 대문자와 소문자
<hide/>
#include <string>
#include <vector>
using namespace std;
string solution(string my_string) {
string answer = "";
size_t uSizeOfMyString = my_string.size();
for (size_t i = 0; i < uSizeOfMyString; ++i)
{
if ('a' <= my_string.at(i) && my_string.at(i) <= 'z')
{
answer += my_string.at(i) - ('a' - 'A');
}
else
{
answer += my_string.at(i) + ('a' - 'A');
}
}
return answer;
}
Ex) Level0 - 직각삼각형 출력하기
<hide/>
#include <iostream>
using namespace std;
int main(void) {
int n;
cin >> n;
for (size_t i = 1; i <= n; ++i)
{
for (size_t j = 1; j <= i; ++j)
{
cout << '*';
}
cout << endl;
}
return 0;
}
Ex) Level0 - 암호 해독
n 번째라서 +1 했음.
<hide/>
#include <string>
#include <vector>
using namespace std;
string solution(string cipher, int code) {
string answer = "";
size_t uSizeOfCipher = cipher.size();
for (size_t i = 0; i < uSizeOfCipher; ++i)
{
if ((i + 1) % code == 0)
{
answer += cipher.at(i);
}
}
return answer;
}
Ex) Level0 - 주사위의 개수
<hide/>
#include <string>
#include <vector>
using namespace std;
#define X (0)
#define Y (1)
#define Z (2)
int solution(vector<int> box, int n) {
int answer = 0;
size_t uWidthCount = box.at(X) / n;
size_t uHeightCount = box.at(Y) / n;
size_t uDepthCount = box.at(Z) / n;
answer = static_cast<int>(uWidthCount * uHeightCount * uDepthCount);
return answer;
}
Ex) Level0 - 가위 바위 보
<hide/>
#include <string>
#include <vector>
using namespace std;
string solution(string rsp) {
string answer = "";
size_t uSizeOfRSP = rsp.size();
for (size_t i = 0; i < uSizeOfRSP; ++i)
{
if ('0' == rsp.at(i))
{
answer += '5';
}
else if ('2' == rsp.at(i))
{
answer += '0';
}
else
{
answer += '2';
}
}
return answer;
}
Ex) Level0 - 문자열 정렬하기 (1)
<hide/>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> solution(string my_string) {
vector<int> answer;
size_t uSizeOfMyString = my_string.size();
for (size_t i = 0; i < uSizeOfMyString; ++i)
{
if ('0' <= my_string.at(i) && my_string.at(i) <= '9')
{
answer.push_back(my_string.at(i) - '0');
}
}
sort(answer.begin(), answer.end());
return answer;
}
Ex) Level0 - 가장 큰 수 찾기
<hide/>
#include <string>
#include <vector>
using namespace std;
#define SIZE (2)
#define MAX_VALUE (0)
#define MAX_VALUE_INDEX (1)
vector<int> solution(vector<int> array) {
vector<int> answer;
answer.resize(SIZE);
answer.at(MAX_VALUE) = array.at(0);
answer.at(MAX_VALUE_INDEX) = 0;
size_t uSizeOfArray = array.size();
for (size_t i = 0; i < uSizeOfArray; ++i)
{
if (answer.at(MAX_VALUE) < array.at(i))
{
answer.at(MAX_VALUE) = array.at(i);
answer.at(MAX_VALUE_INDEX) = i;
}
}
return answer;
}
Ex) Level0 - 최댓값 만들기 (2)
추천수가 많은 풀이를 보니, sort()를 적용 후 가장 작은 두 값의 곱과 가장 큰 두 값의 곱을 max() 걸어버림.
<hide/>
#include <string>
#include <vector>
#include <limits.h>
using namespace std;
int solution(vector<int> numbers) {
int answer = INT_MIN;
size_t uSizeOfNumbers = numbers.size();
for (size_t i = 0; i < uSizeOfNumbers; ++i)
{
for (size_t j = 0; j < uSizeOfNumbers; ++j)
{
if (i == j)
{
continue;
}
if (answer < numbers.at(i) * numbers.at(j))
{
answer = numbers.at(i) * numbers.at(j);
}
}
}
return answer;
}
Ex) Level0 - 피자 나눠 먹기 (2)
<hide/>
#include <string>
#include <vector>
using namespace std;
#define PIECE_COUNT (6)
int solution(int n) {
int answer = 0;
for (size_t i = 1; ; ++i)
{
if ((i * PIECE_COUNT) % n == 0)
{
answer = i;
break;
}
}
return answer;
}
Ex) Level0 - 약수 구하기 [X]
i가 약수이면, n / i도 약수임. 따라서, i <= n / i 전까지만 돌아도 됨. 즉, i * i <= n으로 최적화 가능.
다만, 테스트 케이스 1번이 뭔지 모르겠음. 나중에 재도전.
<hide/>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> solution(int n) {
vector<int> answer;
if (1 == n)
{
answer.push_back(1);
return answer;
}
for (int i = 1; i * i <= n; ++i)
{
if (n % i == 0)
{
answer.push_back(i);
answer.push_back(n / i);
}
}
sort(answer.begin(), answer.end());
return answer;
}
Ex) Level0 - 외계행성의 나이
재귀함수의 활용
<hide/>
#include <string>
#include <vector>
using namespace std;
void TransformAge(int _iCurrentAge, string& _strAnswer)
{
if (_iCurrentAge <= 0)
{
return;
}
TransformAge(_iCurrentAge / 10, _strAnswer);
_strAnswer += 'a' + (_iCurrentAge % 10);
}
string solution(int age) {
string answer = "";
TransformAge(age, answer);
return answer;
}
Ex) Level0 - 인덱스 바꾸기
swap() 함수 사용법을 까먹음.
<hide/>
#include <string>
#include <vector>
using namespace std;
string solution(string my_string, int num1, int num2) {
string answer = my_string;
swap(answer[num1], answer[num2]);
return answer;
}
Ex) Level0 - 숫자 찾기
<hide/>
#include <string>
#include <vector>
using namespace std;
int solution(int num, int k) {
int answer = 0;
int i = 1;
bool bFlagForNone = true;
while (0 < num)
{
if (num % 10 == k)
{
bFlagForNone = false;
answer = i;
}
num /= 10;
++i;
}
if (true == bFlagForNone)
{
return -1;
}
return i - answer;
}
Ex) Level0 - 369게임
<hide/>
#include <string>
#include <vector>
using namespace std;
void GetCount(int _iCurrentOrder, int& _refCount)
{
if (_iCurrentOrder <= 0)
{
return;
}
GetCount(_iCurrentOrder / 10, _refCount);
if (_iCurrentOrder % 10 == 3 || _iCurrentOrder % 10 == 6 || _iCurrentOrder % 10 == 9)
{
++_refCount;
}
}
int solution(int order) {
int answer = 0;
GetCount(order, answer);
return answer;
}
Ex) Level0 - 문자열 정렬하기 (2)
<hide/>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
string solution(string my_string) {
string answer = "";
size_t uSizeOfMyString = my_string.size();
for (size_t i = 0; i < uSizeOfMyString; ++i)
{
if ('A' <= my_string.at(i) && my_string.at(i) <= 'Z')
{
answer += my_string.at(i) + ('a' - 'A');
continue;
}
answer += my_string.at(i);
}
sort(answer.begin(), answer.end());
return answer;
}
Ex) Level0 - 합성수 찾기
i * i 에서 등호 넣어주기.
<hide/>
#include <string>
#include <vector>
using namespace std;
#define BEGIN_COMPOSITE (4)
bool GetDivisor(int _iN)
{
for (int i = 2; i * i <= _iN; ++i)
{
if (_iN % i == 0)
{
return true;
}
}
return false;
}
int solution(int n) {
int answer = 0;
for (int i = BEGIN_COMPOSITE; i <= n; ++i)
{
if (true == GetDivisor(i))
{
++answer;
}
}
return answer;
}
Ex) Level0 - 중복된 문자 제거
<hide/>
#include <string>
#include <vector>
using namespace std;
#define ALPHABET (54)
#define SPACE (53)
#define OFFSET (26)
string solution(string my_string) {
string answer = "";
vector<size_t> vecCountOfAlphabet;
vecCountOfAlphabet.resize(ALPHABET);
size_t uSizeOfMyString = my_string.size();
for (size_t i = 0; i < uSizeOfMyString; ++i)
{
char chAlphabet = my_string.at(i);
if (' ' == chAlphabet)
{
++vecCountOfAlphabet[SPACE];
continue;
}
if ('A' <= chAlphabet && chAlphabet <= 'Z')
{
++vecCountOfAlphabet[chAlphabet - 'A'];
}
else
{
++vecCountOfAlphabet[chAlphabet - 'a' + OFFSET];
}
}
for (size_t i = 0; i < uSizeOfMyString; ++i)
{
char chAlphabet = my_string.at(i);
size_t uIndex = 'a' <= chAlphabet ? chAlphabet - 'a' + OFFSET : chAlphabet - 'A';
if (' ' == chAlphabet)
{
uIndex = SPACE;
}
if (0 != vecCountOfAlphabet[uIndex])
{
answer += my_string.at(i);
vecCountOfAlphabet[uIndex] = 0;
}
}
return answer;
}
Ex) Level0 - 팩토리얼
<hide/>
#include <string>
#include <vector>
using namespace std;
int solution(int n) {
int answer = 1;
unsigned long long llMul = 1;
for (unsigned long long i = 2; ; ++i)
{
llMul *= i;
if (n < llMul)
{
break;
}
answer = static_cast<int>(i);
}
return answer;
}
Ex) Level0 - 모스부호 (1)
string stream을 활용 해보았음.(<sstream>) eof() 함수를 까먹음.
<hide/>
#include <string>
#include <vector>
#include <unordered_map>
#include <sstream>
using namespace std;
string solution(string letter) {
string answer = "";
unordered_map<string, char> m = {
{".-",'a'},{"-...",'b'},{"-.-.",'c'},{"-..",'d'},{".",'e'},{"..-.",'f'},{"--.",'g'},
{"....",'h'},{"..",'i'},{".---",'j'},{"-.-",'k'},{".-..",'l'},{"--",'m'},{"-.",'n'},
{"---",'o'},{".--.",'p'},{"--.-",'q'},{".-.",'r'},{"...",'s'},{"-",'t'},{"..-",'u'},
{"...-",'v'},{".--",'w'},{"-..-",'x'},{"-.--",'y'},{"--..",'z'}
};
stringstream s;
s.str(letter);
string temp;
while (false == s.eof())
{
s >> temp;
answer += m[temp];
}
return answer;
}
Ex) Level0 - A로 B 만들기
솔찍히 풀이에 의구심 드는 문제. 올바르지 풀이인듯.
의도에 맞는 풀이인가..
<hide/>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int solution(string before, string after) {
int answer = 0;
sort(before.begin(), before.end());
sort(after.begin(), after.end());
if (0 == before.compare(after))
{
answer = 1;
}
return answer;
}
Ex) Level0 - 2차원으로 만들기
<hide/>
#include <string>
#include <vector>
using namespace std;
vector<vector<int>> solution(vector<int> num_list, int n) {
vector<vector<int>> answer;
size_t uLine = num_list.size() / n;
answer.resize(uLine, vector<int>(n, 0));
for (size_t i = 0; i < uLine; ++i)
{
for (size_t j = 0; j < n; ++j)
{
answer[i][j] = num_list[i * n + j];
}
}
return answer;
}
Ex) Level0 - k의 개수
<hide/>
#include <string>
#include <vector>
using namespace std;
void Count(int _iNumber, int _iK, int& _refAnswer)
{
while (0 < _iNumber)
{
if (_iNumber % 10 == _iK)
{
++_refAnswer;
}
_iNumber /= 10;
}
}
int solution(int i, int j, int k) {
int answer = 0;
for (;i <= j; ++i)
{
Count(i, k, answer);
}
return answer;
}
Ex) Level0 - 가까운 수
두 수 사이의 거리가 같은 경우엔 더 작은 수를 저장했어야 함.
n이 5이고 배열 안에 3과 4가 있다면, 3을 리턴해야만 함. 근데 순서가 4 3으로 되어 있으면 4가 리턴됨.
그래서 sort()를 한 번 하고 진행.
<hide/>
#include <string>
#include <vector>
#include <algorithm>
#include <limits.h>
using namespace std;
int solution(vector<int> array, int n) {
int answer = 0;
int iMin = INT_MAX;
size_t uSizeOfArray = array.size();
sort(array.begin(), array.end());
for (size_t i = 0; i < uSizeOfArray; ++i)
{
if (abs(array.at(i) - n) < iMin)
{
iMin = abs(array.at(i) - n);
answer = array.at(i);
}
}
return answer;
}
Ex) Level0 - 진료 순서 정하기
랭킹 알고리듬?을 오래간만에 써봄.
<hide/>
#include <string>
#include <vector>
using namespace std;
#define INITIAL_RANK (1)
vector<int> solution(vector<int> emergency) {
vector<int> answer;
size_t uSizeOfEmergency = emergency.size();
answer.resize(uSizeOfEmergency, INITIAL_RANK);
for (size_t i = 0; i < uSizeOfEmergency; ++i)
{
for (size_t j = 0; j < uSizeOfEmergency; ++j)
{
if (emergency[i] < emergency[j])
{
++answer[i];
}
}
}
return answer;
}
Ex) Level0 - 한 번만 등장한 문자
<hide/>
#include <string>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
string solution(string s) {
string answer = "";
set<char> temp;
size_t uSizeOfS = s.size();
for (size_t i = 0 ; i < uSizeOfS; ++i)
{
if (0 == temp.count(s.at(i)))
{
temp.insert(s.at(i));
}
}
size_t uSizeOfTemp = temp.size();
set<char>::iterator iterBegin = temp.begin();
set<char>::iterator iterEnd = temp.end();
while (iterBegin != iterEnd)
{
if (1u == count(s.begin(), s.end(), *iterBegin))
{
answer += *iterBegin;
}
++iterBegin;
}
return answer;
}
댓글