# 생각
누른 숫자를 저장해주고 다음 숫자와의 거리는
(누른 숫자 - 다음 숫자) / 3 + (누른 숫자 - 다음 숫자) % 3으로 구할 수 있다
distL = (tmpL / 3) + (tmpL % 3); distR = (tmpR / 3) + (tmpR % 3);
# 전체 코드
#include <bits/stdc++.h>
using namespace std;
string answer = "";
int L = 10, R = 12, distL = 0, distR = 0;
void funcL(int n)
{
answer += 'L';
L = n;
}
void funcR(int n)
{
answer += 'R';
R = n;
}
string solution(vector<int> numbers, string hand) {
for (int i = 0; i < numbers.size(); i++)
{
if (numbers[i] == 1 or numbers[i] == 4 or numbers[i] == 7)
{
funcL(numbers[i]);
}
else if (numbers[i] == 3 or numbers[i] == 6 or numbers[i] == 9)
{
funcR(numbers[i]);
}
else
{
if (numbers[i] == 0) numbers[i] = 11;
int tmpL = abs(L - numbers[i]), tmpR = abs(R - numbers[i]);
distL = (tmpL / 3) + (tmpL % 3); distR = (tmpR / 3) + (tmpR % 3);
if (distL == distR)
{
if (hand == "left")
{
funcL(numbers[i]);
}
else
{
funcR(numbers[i]);
}
}
else if (distL < distR)
{
funcL(numbers[i]);
}
else
{
funcR(numbers[i]);
}
}
}
return answer;
}
출처: 프로그래머스 코딩 테스트 연습, https://programmers.co.kr/learn/challenges
'Algorithm > 프로그래머스' 카테고리의 다른 글
[Lv.1] 완주하지 못한 선수 with cpp (0) | 2022.02.28 |
---|---|
[Lv.1] 크레인 인형뽑기 게임 with cpp (0) | 2022.02.20 |
[Lv.1] 신규 아이디 추천 with cpp (0) | 2022.02.10 |
[Lv.1] 로또의 최고 순위와 최저 순위 with cpp (0) | 2022.02.09 |
[Lv.1] 신고 결과 받기 with cpp (0) | 2022.01.29 |