We will find a way, we always have.

-interstellar

Problem Solving/๋ฐฑ์ค€

[๋ฐฑ์ค€] 13140๋ฒˆ: Hello World! - ํŒŒ์ด์ฌ

Redddy 2022. 6. 27. 11:32

๐Ÿ”ˆ ๋ฌธ์ œ

N์ด ์ฃผ์–ด์งˆ ๋•Œ hello + world = N์„ ๋งŒ์กฑํ•˜๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ํ•œ ์ž๋ฆฌ ์ž์—ฐ์ˆ˜(0 ํฌํ•จ) d, e, h, l, o, r, w๋ฅผ ๊ตฌํ•ด์„œ ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์—ฌ๋ผ. ๋‹จ, h์™€ w๋Š” 0์ด ๋  ์ˆ˜ ์—†๋‹ค.

hello world

 

๐Ÿ“์ž…๋ ฅ

์ฒซ ๋ฒˆ์งธ ์ค„์— ์–‘์˜ ์ •์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค. N์€ 32๋น„ํŠธ ์ •์ˆ˜ ๋ฒ”์œ„๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ์ˆ˜๋งŒ ๋“ค์–ด์˜จ๋‹ค.

 

๐Ÿ“‘์ถœ๋ ฅ

๋งŒ์•ฝ ๋‹ต์ด ์—†์œผ๋ฉด 'No Answer'์„ ์ถœ๋ ฅํ•œ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ ๊ฐ€๋Šฅํ•œ ๋‹ต๋“ค ์ค‘์—์„œ ์•„๋ฌด๊ฑฐ๋‚˜ ์ถœ๋ ฅํ•œ๋‹ค.

 

๐Ÿ“š ๋ฌธ์ œ ํ•ด์„

์ฒ˜์Œ์—๋Š” l์ด ๊ฐ™์€ ์œ„์น˜์— ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์ด์šฉํ•˜์—ฌ, n[-2]//2 ๋กœ l์„ ๋จผ์ € ๊ตฌํ•œ๋‹ค์Œ, n[-3] - l ์„ ํ†ตํ•ด (2*l ์— ์บ๋ฆฌ๊ฐ’์ด ์žˆ๋Š”์ง€ ํ™•์ธํ›„) r ์„ ๊ตฌํ•˜๊ณ , o์™€ d๋Š” for ๋ฌธ์„ ๋Œ๋ ค ๊ตฌํ•œ๋’ค, e๋ฅผ ๊ตฌํ•˜๊ณ  ๋งˆ์ง€๋ง‰์œผ๋กœ h,w ๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜์˜€๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค ํŒ์ •์„ ๋ฐ›์•˜๋‹ค.

์ด์œ ๋Š” n[-2] = 4 ์ผ๋•Œ l์€ 2๊ฐ€ ๋ ์ˆ˜๋„ ์žˆ๊ณ , 7์ด ๋  ์ˆ˜๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. 

์ด ๋‘๊ฐ€์ง€ ๊ฒฝ์šฐ ๋ชจ๋‘ ์ฒดํฌํ•ด์ค˜์•ผ ํ•˜๋Š”๋ฐ ๋‚ด ์ฝ”๋“œ๋Š” 2์ผ ๊ฒฝ์šฐ๋งŒ ์ฒดํฌํ•ด์คฌ๊ธฐ์— ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค ํŒ์ •์„ ๋ฐ›์•˜๋˜ ๊ฒƒ์ด๋‹ค. (์ด๋ฐฉ๋ฒ•์ˆ˜์ •ํ•ด์„œ ๋‚˜์ค‘์— ๋‹ค์‹œ ํ’€์–ด๋ด์•ผ์ง€)

๊ทธ๋ฆฌ๊ณ  ๋‹ค๋ฅธ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด์„œ ๋ฌธ์ œ์ ์„ ๋˜ ํ•˜๋‚˜ ๋ฐœ๊ฒฌํ–ˆ๋Š”๋ฐ, ๋ฌธ์ž๊ฐ€ ์ค‘๋ณต๋˜์ง€ ์•Š์•„์•ผ ํ•˜๋Š”๋ฐ, ๋‚ด ์ฝ”๋“œ๋Š” ๊ทธ ๊ฒฝ์šฐ๋ฅผ ์‹ ๊ฒฝ์จ์ฃผ์ง€ ์•Š์•˜๋‹ค. 

 

์ƒˆ๋กœ์šด ํ’€์ด ๋ฐฉ๋ฒ•์€ ์ •๋ง ๋ง ๊ทธ๋Œ€๋กœ ๋ธŒ๋ฃจํŠธํฌ์Šค ๊ทธ ์ž์ฒด์ด๋‹ค.

0 ~ 9 ๊นŒ์ง€์˜ ์ˆ˜ ์ค‘์—์„œ ์ค‘๋ณต ์—†์ด 7๊ฐœ์˜ ์ˆซ์ž๋ฅผ ๊ณ ๋ฅด๊ณ  hello์™€ world ๋ฅผ ๋งŒ๋“ค์–ด์„œ n๊ณผ ๊ฐ™์œผ๋ฉด ์ถœ๋ ฅํ•ด์ฃผ๊ณ  ๋ชจ๋‘ ์กฐํ•ฉํ•ด์„œ ์ฒดํฌํ•˜์˜€๋Š”๋ฐ n๊ณผ ๊ฐ™์ง€ ์•Š๋‹ค๋ฉด No Answer๋ฅผ ์ถœ๋ ฅํ•ด์ฃผ๋ฉด ๋˜๋Š” ๋ฌธ์ œ์˜€๋‹ค.

 

 

๐Ÿ’ป ์ฝ”๋“œ

import itertools, sys
input = sys.stdin.readline

nums = [1,2,3,4,5,6,7,8,9,0]
n = int(input().rstrip())
for s in itertools.permutations(nums, 7):
    h,e,l,o,w,r,d = s # s๋Š” nums ์—์„œ ์ค‘๋ณต๋˜์ง€ ์•Š๊ฒŒ 7๊ฐœ๋ฅผ ๋ฝ‘์€๊ฒƒ
    hello = 10000*h + 1000*e + 100*l + 10*l + o
    world = 10000*w + 1000*o + 100*r + 10*l + d
    # hello๋ž‘ world ๋”ํ•œ ๊ฒƒ์ด ์ž…๋ ฅ๊ฐ’๊ณผ ๊ฐ™์œผ๋ฉด์„œ h์™€ w๊ฐ€ 0์ด ์•„๋‹ˆ๋ผ๋ฉด
    if hello + world == n and h != 0 and w != 0:
        # ์ถœ๋ ฅ ํ˜•์‹์— ๋งž๊ฒŒ ์ถœ๋ ฅ
        print(f"  {hello}")
        print(f"+ {world}")
        print('-'*7)
        print(str(n).rjust(7))
        exit() # ์ข…๋ฃŒ
# for ๋ฌธ์„ ๋‹ค ๋Œ์•˜๋Š”๋ฐ๋„ ํ•ด๋‹น ๊ฐ’์ด ์—†์„ ๊ฒฝ์šฐ No Answer
print("No Answer")

 

๐Ÿญ ์‹œํ–‰์ฐฉ์˜ค

์‹œํ–‰ ์ฐฉ์˜ค

 

์‹ค์ˆ˜์ฝ”๋“œ

n = list(input())
# print(n)
if len(n) != 5 and len(n) != 6:
    print("No Answer")
    exit()

if int(n[-2]) == 0:
    l = 5
else:
    l = int(n[-2])//2

if 2*l > 9:
    n3 = int(n[-3]) -1
else:
    n3 = int(n[-3])

if l <= n3:
    r = n3 - l
else:
    r = 10+ n3 - l

for i in range(9):
    for j in range(9):
        if int(n[-2])%2 == 0: # ๋‘๋ฒˆ์งธ ์ž๋ฆฌ๊ฐ€ ์ง์ˆ˜๋ผ๋ฉด
            if i+j < 10 and i+j == int(n[-1]):
                o = i
                d = j
                break
        else:
            if i+j > 9 and i+j == int(str(1)+n[-1]):
                o = i
                d = j
                break

if int(str(l)+str(l)+str(o))+int(str(r)+str(l)+str(d)) > 999:
    n4 = int(n[-4]) -1
else:
    n4 = int(n[-4])

if o <= n4:
    e = n4 - o
else:
    e = 10 + n4 - o

if int(str(e)+str(l)+str(l)+str(o)) + int(str(o)+str(r)+str(l)+str(d)) > 9999:
    n5 = int(n[-5]) - 1
else:
    n5 = int(n[-5])

for i in range(1,9):
    for j in range(1, 9):
        if len(n) == 5:
            if i+j == n5 and i+j < 10:
                h = i
                w = j
                break
        else:
            if i+j == (n5+10) and i+j >9:
                h = i
                w = j
                break
try:
    print('  ',h,e,l,l,o, sep='')
    print('+ ',w,o,r,l,d,sep='')
    print('-'*7)
    print(''.join(n).rjust(7))
except:
    print("No Answer")

 

๐Ÿ”— ๋ฌธ์ œ๋งํฌ Hello World

 

13140๋ฒˆ: Hello World!

N์ด ์ฃผ์–ด์งˆ ๋•Œ hello + world = N์„ ๋งŒ์กฑํ•˜๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ํ•œ ์ž๋ฆฌ ์ž์—ฐ์ˆ˜(0 ํฌํ•จ) d, e, h, l, o, r, w๋ฅผ ๊ตฌํ•ด์„œ ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์—ฌ๋ผ. ๋‹จ, h์™€ w๋Š” 0์ด ๋  ์ˆ˜ ์—†๋‹ค.

www.acmicpc.net