J1Yun
ZU-TECHLOG
J1Yun
์ „์ฒด ๋ฐฉ๋ฌธ์ž
์˜ค๋Š˜
์–ด์ œ
  • ๐Ÿ“‘ Category (135)
    • Algorithm (61)
      • ๐Ÿ“š Concept (6)
      • ๐Ÿ“˜ Baekjoon Judge (53)
      • ๐Ÿ“— Programmers (2)
    • Computer Science (42)
      • ๐Ÿ”’ Operating System (14)
      • ๐Ÿ“ก Network (15)
      • ๐Ÿ’พ Database (8)
      • ๐Ÿงฉ Design Pattern (4)
      • ๐Ÿ”‘ Security (1)
    • Activities (12)
      • ๐Ÿฆ ๋ฉ‹์Ÿ์ด์‚ฌ์ž์ฒ˜๋Ÿผ 9๊ธฐ (6)
      • ๐Ÿ’ป SW๋งˆ์—์ŠคํŠธ๋กœ 13๊ธฐ (6)
    • Infra (1)
      • โ˜๏ธ AWS (1)
    • Languages (1)
      • ๐Ÿ’™ Python (1)
    • Backend (7)
      • ๐Ÿ”ต Django (4)
      • ๐ŸŸข Node.js (3)
    • Ect. (8)
      • ๐Ÿ’ฌ Talk (0)
      • ๐Ÿ—‚๏ธ ๊ฐœ๋ฐœ์ง๊ตฐ ์ทจ์—… ์ค€๋น„์ž๋ฃŒ (8)

๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

  • ํ™ˆ
  • ํƒœ๊ทธ
  • ๋ฐฉ๋ช…๋ก

๊ณต์ง€์‚ฌํ•ญ

์ธ๊ธฐ ๊ธ€

์ตœ๊ทผ ๋Œ“๊ธ€

์ตœ๊ทผ ๊ธ€

ํ‹ฐ์Šคํ† ๋ฆฌ

250x250
hELLO ยท Designed By ์ •์ƒ์šฐ.
J1Yun

ZU-TECHLOG

[BOJ] ๋ฐฑ์ค€ 16953๋ฒˆ A๋ฅผ B๋กœ ๋ฐ”๊พธ๊ธฐ (A->B) - ํŒŒ์ด์ฌ(Python)
Algorithm/๐Ÿ“˜ Baekjoon Judge

[BOJ] ๋ฐฑ์ค€ 16953๋ฒˆ A๋ฅผ B๋กœ ๋ฐ”๊พธ๊ธฐ (A->B) - ํŒŒ์ด์ฌ(Python)

728x90

๋ฌธ์ œ

์ •์ˆ˜ A๋ฅผ B๋กœ ๋ฐ”๊พธ๋ ค๊ณ  ํ•œ๋‹ค. ๊ฐ€๋Šฅํ•œ ์—ฐ์‚ฐ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋‘ ๊ฐ€์ง€์ด๋‹ค.

  • 2๋ฅผ ๊ณฑํ•œ๋‹ค.
  • 1์„ ์ˆ˜์˜ ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ์— ์ถ”๊ฐ€ํ•œ๋‹ค. 

A๋ฅผ B๋กœ ๋ฐ”๊พธ๋Š”๋ฐ ํ•„์š”ํ•œ ์—ฐ์‚ฐ์˜ ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•ด๋ณด์ž.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— A, B (1 ≤ A < B ≤ 109)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

์ถœ๋ ฅ

A๋ฅผ B๋กœ ๋ฐ”๊พธ๋Š”๋ฐ ํ•„์š”ํ•œ ์—ฐ์‚ฐ์˜ ์ตœ์†Ÿ๊ฐ’์— 1์„ ๋”ํ•œ ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค. ๋งŒ๋“ค ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” -1์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

https://www.acmicpc.net/problem/16953

 

16953๋ฒˆ: A → B

์ฒซ์งธ ์ค„์— A, B (1 ≤ A < B ≤ 109)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

www.acmicpc.net

 

๐Ÿ’ก ํ’€์ด ๋ฐ ์ฝ”๋“œ

a, b = map(int, input().split())

count = 0
while b > a:
    if b % 10 == 1:
        b //= 10
    elif b % 2 == 0:
        b /= 2
    else :
        break
    count += 1

if a == b:
    print(count + 1)
else:
    print(-1)

  A๋ฅผ B๋กœ ๋ฐ”๊ฟ€ ๋•Œ๋Š” 2๋ฅผ ๊ณฑํ•˜๊ฑฐ๋‚˜ ์˜ค๋ฅธ์ชฝ์— 1์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ๋ฐ–์— ์—†์œผ๋ฏ€๋กœ B๋Š” A๋ณด๋‹ค ํ•ญ์ƒ ํฌ๋‹ค. ๋”ฐ๋ผ์„œ, B๊ฐ€ A๋ณด๋‹ค ํด ๋•Œ ๋™์•ˆ B์˜ ๋์ž๋ฆฌ๊ฐ€ 1์ด๋ฉด 1์„ ๋–ผ์–ด์ฃผ๊ณ , B๊ฐ€ 2๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ์ง์ˆ˜์ด๋ฉด 2๋กœ ๋‚˜๋ˆ ์ค€๋‹ค. ๋‘ ๊ฐ€์ง€ ์—ฐ์‚ฐ์ด ๋ชจ๋‘ ๋ถˆ๊ฐ€๋Šฅํ•  ๋•Œ๋Š” ๋ฐ”๋กœ ๋ฐ˜๋ณต๋ฌธ์„ ๋น ์ ธ๋‚˜์˜จ๋‹ค. ๋ฐ˜๋ณต๋ฌธ(while)์ด ๋๋‚˜๊ณ  A์™€ B๊ฐ€ ๊ฐ™์€ ์ˆ˜๋ผ๋ฉด ์—ฐ์‚ฐํšŸ์ˆ˜์ธ count์— 1์„ ๋”ํ•ด ์ถœ๋ ฅํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด A๋Š” B๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์—†์Œ์„ ์˜๋ฏธํ•˜๋ฏ€๋กœ -1์„ ์ถœ๋ ฅํ•œ๋‹ค.

728x90

'Algorithm > ๐Ÿ“˜ Baekjoon Judge' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[BOJ] ๋ฐฑ์ค€ 1149๋ฒˆ RGB๊ฑฐ๋ฆฌ - ํŒŒ์ด์ฌ(Python)  (0) 2021.09.14
[BOJ] ๋ฐฑ์ค€ 1463๋ฒˆ 1๋กœ ๋งŒ๋“ค๊ธฐ - ํŒŒ์ด์ฌ(Python)  (0) 2021.09.10
[BOJ] ๋ฐฑ์ค€ 1715๋ฒˆ ์นด๋“œ ์ •๋ ฌํ•˜๊ธฐ - ํŒŒ์ด์ฌ(Python)  (0) 2021.08.26
[BOJ] ๋ฐฑ์ค€ 1789๋ฒˆ ์ˆ˜๋“ค์˜ ํ•ฉ - ํŒŒ์ด์ฌ(Python)  (0) 2021.08.26
[BOJ] ๋ฐฑ์ค€ 2473๋ฒˆ ์„ธ ์šฉ์•ก - ํŒŒ์ด์ฌ(Python)  (3) 2021.08.24
    'Algorithm/๐Ÿ“˜ Baekjoon Judge' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€์ด๋‹ค
    • [BOJ] ๋ฐฑ์ค€ 1149๋ฒˆ RGB๊ฑฐ๋ฆฌ - ํŒŒ์ด์ฌ(Python)
    • [BOJ] ๋ฐฑ์ค€ 1463๋ฒˆ 1๋กœ ๋งŒ๋“ค๊ธฐ - ํŒŒ์ด์ฌ(Python)
    • [BOJ] ๋ฐฑ์ค€ 1715๋ฒˆ ์นด๋“œ ์ •๋ ฌํ•˜๊ธฐ - ํŒŒ์ด์ฌ(Python)
    • [BOJ] ๋ฐฑ์ค€ 1789๋ฒˆ ์ˆ˜๋“ค์˜ ํ•ฉ - ํŒŒ์ด์ฌ(Python)
    J1Yun
    J1Yun
    ๊ฐœ๋ฐœ ๊ด€๋ จ ๊ธฐ์ˆ  ๋ฐ ๊ณต๋ถ€ ๋‚ด์šฉ ๊ธฐ๋ก์žฅ

    ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”