ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ๋ฌธ์ œ

 

1057๋ฒˆ: ํ† ๋„ˆ๋จผํŠธ

๊น€์ง€๋ฏผ์€ N๋ช…์ด ์ฐธ๊ฐ€ํ•˜๋Š” ์Šคํƒ€ ํ† ๋„ˆ๋จผํŠธ์— ์ง„์ถœํ–ˆ๋‹ค. ํ† ๋„ˆ๋จผํŠธ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ง„ํ–‰๋œ๋‹ค. ์ผ๋‹จ N๋ช…์˜ ์ฐธ๊ฐ€์ž๋Š” ๋ฒˆํ˜ธ๊ฐ€ 1๋ฒˆ๋ถ€ํ„ฐ N๋ฒˆ๊นŒ์ง€ ๋ฐฐ์ •๋ฐ›๋Š”๋‹ค. ๊ทธ๋Ÿฌ๊ณ  ๋‚œ ํ›„์— ์„œ๋กœ ์ธ์ ‘ํ•œ ๋ฒˆํ˜ธ๋ผ๋ฆฌ ์Šคํƒ€๋ฅผ

www.acmicpc.net


โœ๐Ÿป ํ’€์ด

๐ŸŽจ Go

// https://www.acmicpc.net/problem/1057
package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n, jimin, hansu int
	fmt.Fscanln(reader, &n, &jimin, &hansu)
	var round int
	for round < n {
		round++
		if jimin%2 == 0 {
			jimin /= 2
		} else {
			jimin = jimin/2 + 1
		}
		if hansu%2 == 0 {
			hansu /= 2
		} else {
			hansu = hansu/2 + 1
		}
		if jimin == hansu {
			fmt.Fprintln(writer, round)
			return
		}
	}
	fmt.Fprintln(writer, -1)
}

๐ŸŽจ Python3

# https://www.acmicpc.net/problem/1057
import sys

if __name__ == "__main__":
    n, jimin, hansu = list(map(int, sys.stdin.readline().split()))
    round = 0
    while round < n:
        round += 1
        if jimin%2 == 0:
            jimin //= 2
        else:
            jimin = jimin//2 + 1
        if hansu%2 == 0:
            hansu //= 2
        else:
            hansu = hansu//2 + 1
        if jimin == hansu:
            print(round)
            sys.exit()
    print(-1)
728x90
๋Œ“๊ธ€