728x90
๐๋ฌธ์ ๋ณด๋ฌ๊ฐ๊ธฐ
๐จ๐ปํ์ด ๊ณผ์
- ๋ด๋ ค๊ฐ๋ ๊ฒ ์ ๋ฆฌํ ์ซ์ : 0, 1, 2, 3, 4
- ์ฌ๋ผ๊ฐ๋ ๊ฒ ์ ๋ฆฌํ ์ซ์ : 6, 7, 8, 9
- ์ ๋งคํ ์ซ์ : 5
์ซ์ 5๊ฐ ๋์์ ๋์๋ ๋ฐ๋ก ์ ์ซ์๋ฅผ ์ดํด๋ณด๋ ๊ฒ ํ์ํฉ๋๋ค. ๋ฐ๋ก ์ ์ซ์๊ฐ 5 ์ด์์ด๋ผ๋ฉด ์ฌ๋ผ๊ฐ๋ ๊ฒ ์ ๋ฆฌํ๊ณ , 5 ๋ฏธ๋ง์ด๋ผ๋ฉด ๋ด๋ ค๊ฐ๋ ๊ฒ ์ ๋ฆฌํฉ๋๋ค.
# [์์1] 35
- ์ฌ๋ผ๊ฐ ๊ฒฝ์ฐ : 35 + 5 -> 40 - 40 => 9๋ฒ ์ฌ์ฉ
- ๋ด๋ ค๊ฐ ๊ฒฝ์ฐ : 35 - 5 -> 30 - 30 => 8๋ฒ ์ฌ์ฉ
3์ 5๋ณด๋ค ์์ผ๋ฏ๋ก ๋ด๋ ค๊ฐ๋ ๊ฒ ์ ๋ฆฌํฉ๋๋ค.
# [์์2] 65
- ์ฌ๋ผ๊ฐ ๊ฒฝ์ฐ : 65 + 5 -> 70 + 30 -> 100 - 100 => 9๋ฒ ์ฌ์ฉ
- ๋ด๋ ค๊ฐ ๊ฒฝ์ฐ : 65 - 5 -> 60 + 40 -> 100 - 100 => 10๋ฒ ์ฌ์ฉ
6์ 5๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ์ผ๋ฏ๋ก ์ฌ๋ผ๊ฐ๋ ๊ฒ ์ ๋ฆฌํฉ๋๋ค.
โ๏ธ์์ค ์ฝ๋ ๋ฐ ๊ฒฐ๊ณผ
#include <string>
using namespace std;
const int HALF_NUM = 5;
const int divider = 10;
bool IsWillGoUp(int quotient, int remainder) {
if (remainder == HALF_NUM) {
int nextRemainder = quotient % divider;
return nextRemainder >= HALF_NUM;
}
return remainder > HALF_NUM;
}
int solution(int storey) {
int magicStoneCount = 0;
while (true) {
int quotient = storey / divider;
int remainder = storey % divider;
bool isWillGoUp = IsWillGoUp(quotient, remainder);
int direction = isWillGoUp ? 1 : -1;
int moveDistance = min(10 - remainder, remainder);
storey += (moveDistance * direction);
magicStoneCount += moveDistance;
int nextFloorQuotient = storey / divider;
if (nextFloorQuotient == 0)
break;
storey = nextFloorQuotient;
}
return magicStoneCount;
}
728x90
๋ฐ์ํ
'๐คAlgorithm > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Programmers] Lv2. ๋ฆฌ์ฝ์ณ ๋ก๋ด | C++ (0) | 2023.06.07 |
---|---|
[Programmers] Lv2. ๊ด๋ฌผ ์บ๊ธฐ | C++ (0) | 2023.06.06 |
[Programmers] Lv2. ๋ค์ ์๋ ํฐ ์ ์ฐพ๊ธฐ | C++ (0) | 2023.06.04 |
[Programmers] Lv2. ๋ชจ์ ์ฌ์ | C++ (0) | 2023.06.04 |
[Programmers] Lv2. ํธํ ๋์ค (0) | 2023.06.03 |