๋ณธ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ
728x90
๐Ÿ”—๋ฌธ์ œ ๋ณด๋Ÿฌ๊ฐ€๊ธฐ
 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

 

๐Ÿ‘จ‍๐Ÿ’ปํ’€์ด ๊ณผ์ •

  • ๋‚ด๋ ค๊ฐ€๋Š” ๊ฒŒ ์œ ๋ฆฌํ•œ ์ˆซ์ž : 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
๋ฐ˜์‘ํ˜•