Ex) Level0 - 캐릭터의 좌표
<hide/>
#include <string>
#include <vector>
using namespace std;
vector<int> solution(vector<string> keyinput, vector<int> board) {
vector<int> answer;
answer.resize(2, 0);
size_t uSizeOfKeyInput = keyinput.size();
for (size_t i = 0; i < uSizeOfKeyInput; ++i)
{
switch (keyinput[i][0])
{
case 'u':
answer[1] += +1;
answer[1] = max(answer[1], -(board[1] / 2));
answer[1] = min(answer[1], (board[1] / 2));
break;
case 'd':
answer[1] += -1;
answer[1] = max(answer[1], -(board[1] / 2));
answer[1] = min(answer[1], (board[1] / 2));
break;
case 'l':
answer[0] += -1;
answer[0] = max(answer[0], -(board[0] / 2));
answer[0] = min(answer[0], (board[0] / 2));
break;
case 'r':
answer[0] += +1;
answer[0] = max(answer[0], -(board[0] / 2));
answer[0] = min(answer[0], (board[0] / 2));
break;
default:
break;
}
}
return answer;
}
Ex) Level0 - 삼각형의 완성조건 (2)
<hide/>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
int solution(vector<int> sides) {
int answer = 0;
int iMax, iSum = 0;
int iEnd = accumulate(sides.begin(), sides.end(), 0);
for (int i = 1; i < iEnd ; ++i)
{
sides.push_back(i);
iMax = *(max_element(sides.begin(), sides.end()));
iSum = accumulate(sides.begin(), sides.end(), 0);
if (iMax < iSum - iMax)
{
++answer;
}
sides.pop_back();
}
return answer;
}
Ex) Level0 - 로그인 성공?
map.insert() 함수와 map.find() 함수의 사용법을 알 수 있었음.
<hide/>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
string solution(vector<string> id_pw, vector<vector<string>> db) {
string answer = "";
map<string, string> m;
size_t uSizeOfDB = db.size();
for (size_t i = 0; i < uSizeOfDB; ++i)
{
m.insert({db[i][0], db[i][1]});
}
map<string, string>::iterator iterFind = m.find(id_pw[0]);
if (m.end() != iterFind)
{
if (iterFind->second == id_pw[1])
{
return "login";
}
else
{
return "wrong pw";
}
}
else
{
return "fail";
}
return answer;
}
Ex) Level0 - 직사각형 넓이 구하기
<hide/>
#include <string>
#include <vector>
using namespace std;
#define DOT_COUNT (4)
#define DOT_SIZE (2)
int solution(vector<vector<int>> dots) {
int answer = 0;
int iPivotX = dots[0][0];
int iWidth;
int iPivotY = dots[0][1];
int iHeight;
for (size_t i = 1; i < DOT_COUNT; ++i)
{
if (iPivotX == dots[i][0])
{
continue;
}
else
{
iWidth = abs(dots[i][0] - iPivotX);
}
}
for (size_t i = 1; i < DOT_COUNT; ++i)
{
if (iPivotY == dots[i][1])
{
continue;
}
else
{
iHeight = abs(dots[i][1] - iPivotY);
}
}
answer = iWidth * iHeight;
return answer;
}
Ex) Level0 - 문자열 밀기
<hide/>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int solution(string A, string B) {
int answer = -1;
if (A == B)
{
return 0;
}
string strCloneA = A;
size_t uSizeOfA = A.size();
for (size_t i = 0; i < uSizeOfA; ++i)
{
string ch = "";
ch += strCloneA[uSizeOfA - 1];
strCloneA.replace(strCloneA.begin() + 1, strCloneA.end(), strCloneA.begin(), strCloneA.end() -1);
strCloneA.replace(0, 1, ch);
if (strCloneA == B)
{
answer = i + 1;
break;
}
}
return answer;
}
Ex) Level0 - 치킨 쿠폰 [X]
재귀함수를 이용해보고자 함.
<hide/>
#include <string>
#include <vector>
using namespace std;
void Recursive(int _iOrderCount, int& _refiCouponCount, int& _refiAnswer)
{
if (_iOrderCount <= 0)
{
return;
}
// 쿠폰 갯수
_refiCouponCount += _iOrderCount;
// 쿠폰 사용
_refiAnswer += _refiCouponCount / 10;
_refiCouponCount %= 10;
// 주문 업데이트
_iOrderCount = _iOrderCount / 10;
Recursive(_iOrderCount, _refiCouponCount, _refiAnswer);
}
int solution(int chicken) {
int answer = 0;
int iCouponCount = 0;
Recursive(chicken, iCouponCount, answer);
return answer;
}
Ex) Level0 - 유한소수 판별하기
GCD 코드는 외우자.
<hide/>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void CalculateGCD(int _iA, int _iB, int& _refiGCD)
{
int iRemain = 0;
while (_iB != 0)
{
iRemain = _iA % _iB;
_iA = _iB;
_iB = iRemain;
}
_refiGCD = _iA;
}
int solution(int a, int b) {
int answer = 1;
int iGCD = 0;
CalculateGCD(a, b, iGCD);
a /= iGCD;
b /= iGCD;
cout << a << " : " << b << endl;
vector<int> vDivisors;
for (int i = 1; i * i <= b; ++i)
{
if (b % i == 0)
{
vDivisors.push_back(i);
vDivisors.push_back(b / i);
}
}
size_t uSizeOfDivisors = vDivisors.size();
for (size_t i = 0; i < uSizeOfDivisors; ++i)
{
cout << vDivisors[i] << ' ';
bool IsPrimeNumber = true;
for (int j = 2; j < vDivisors[i]; ++j)
{
if (vDivisors[i] % j == 0)
{
IsPrimeNumber = false;
}
}
if (true == IsPrimeNumber && 1 != vDivisors[i] && 2 != vDivisors[i] && 5 != vDivisors[i])
{
answer = 2;
}
}
return answer;
}
Ex) Level0 - 등수 매기기 [X]
<hide/>
#include <string>
#include <vector>
using namespace std;
vector<int> solution(vector<vector<int>> score) {
vector<int> answer;
answer.resize(score.size(), 1);
size_t uSizeOfScore = score.size();
for (size_t i = 0; i < uSizeOfScore; ++i)
{
score[i][0] = (score[i][0] + score[i][1]) / 2;
}
for (size_t i = 0; i < uSizeOfScore; ++i)
{
for (size_t j = 0; j < uSizeOfScore; ++j)
{
if (i == j)
{
continue;
}
else
{
if (score[i][0] < score[j][0])
{
++answer[i];
}
}
}
}
return answer;
}
댓글