이런저런 IT 이야기
article thumbnail
Published 2023. 5. 27. 23:49
Programming Challenges #37 Algorithm
반응형

Stan and Ollie play the game of multiplication by multiplying an integer p by one of the numbers 2 to 9. Stan always starts with p = 1, does his multiplication, then Ollie multiplies the number, then Stan and so on. Before a game starts, they draw an integer 1 < n < 4294967295 and the winner is who first reaches p n.

Stan과 Ollie가 정수 p 에 2 이상 9이하의 수 가운데 하나를 곱하는 곱하기 게임을 한다. 항상 가장 먼저 게임을 시작하는 것은 스탠으로, p=1에서 시작해서 곱하기를 한다. 그러면 올리는 그 수를 받아서 곱셈을 한 다음 다시 스탠한테 순서를 넘기고, 이런 과정이 반복된다. 게임을 시작하기 전에 무작위로 1보다 크고 4,294,967,295보다 작은 정수를 하나뽑는데, 둘 중에서 n이상인 p를 먼저 만들어내는 사람이 게임의 승자가 된다. 이때 스탠과 올리는 완벽하게 게임을 한다고 가정하자.

  1. Stan이 무조건 먼저 시작한다.
  2. 누가 이기기 위한 조건을 만들지 않는다.
  3. 현재 조건에서 내가 가지고 있는 숫자의 범위에서 곱한값이 n값을 넘어 갈수 있는지를 확인한다.
function calculator(n) {
    console.log(`Input : ${n}`)
    let turn = 0;
    let min = 1;
    let max = 1;
    while (max < n) {
        turn = 1 - turn;
        min *= 2;
        max *= 9;
        console.log(`who : ${turn ? "Stan " : "Ollie"} | min : ${min} | max : ${max}`)
    }
    console.log(`answer : ${turn ? "Stan" : "Ollie"}`)
    console.log("---------------------------------------------");
}
calculator(162);
calculator(17);
calculator(34012226);

출력

Input : 162
 who : Stan  | min : 2 | max : 9
 who : Ollie | min : 4 | max : 81
 who : Stan  | min : 8 | max : 729
answer : Stan
---------------------------------------------
Input : 17
 who : Stan  | min : 2 | max : 9
 who : Ollie | min : 4 | max : 81
answer : Ollie
---------------------------------------------
Input : 34012226
 who : Stan  | min : 2 | max : 9
 who : Ollie | min : 4 | max : 81
 who : Stan  | min : 8 | max : 729
 who : Ollie | min : 16 | max : 6561
 who : Stan  | min : 32 | max : 59049
 who : Ollie | min : 64 | max : 531441
 who : Stan  | min : 128 | max : 4782969
 who : Ollie | min : 256 | max : 43046721
answer : Ollie
---------------------------------------------

반응형
profile

이런저런 IT 이야기

@이런저런 IT 이야기

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!

profile on loading

Loading...

검색 태그