[Python/ํŒŒ์ด์ฌ] [๐Ÿฅˆ2] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 9020 - ๊ณจ๋“œ๋ฐ”ํ์˜ ์ถ”์ธก

keyneneยท2022๋…„ 10์›” 19์ผ
0

Python

๋ชฉ๋ก ๋ณด๊ธฐ
13/26
post-custom-banner

๐Ÿ“–[Python/ํŒŒ์ด์ฌ][๐Ÿฅˆ2] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 9020 - ๊ณจ๋“œ๋ฐ”ํ์˜ ์ถ”์ธก

๐Ÿ“œ๋ฌธ์ œ



๐Ÿ“•ํ’€์ด๋ฐฉํ–ฅ

N์ด 2์˜ ๋ฐฐ์ˆ˜์ธ๋ฐ๋‹ค, N์˜ ๋ฒ”์œ„๋„ ๋งŒ๋งŒํ•ด์„œ 2~n๊นŒ์ง€ 2์˜๋ฐฐ์ˆ˜๋งŒ ๋ชจ๋‘ ์†Œ์ˆ˜๋ฅผ ๊ตฌํ•ด ์ €์žฅํ•ด๋†“๊ณ ,
abs(์ ˆ๋Œ“๊ฐ’)ํ•จ์ˆ˜๋กœ N์˜ ์†Œ์ˆ˜๋“ค์„ ๋น„๊ตํ•˜๋ฉด์„œ ์ถœ๋ ฅํ•  ์ƒ๊ฐ์„ ํ•˜๋ฉด ํฐ ์ฝ” ๋‹ค์นœ๋‹ค...๐Ÿคฆ๐Ÿปโ€โ™€๏ธ

๋‚ด ํฌ์ŠคํŒ… : [Python] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 4948 - ๋ฒ ๋ฅดํŠธ๋ž‘ ๊ณต์ค€
์ด์ „ ํฌ์ŠคํŒ…์—์„œ ์œ„ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•œ ๊ฒฝํ—˜์œผ๋กœ๋ถ€ํ„ฐ ์†Œ์ˆ˜๋ฅผ ๋ฆฌ์ŠคํŠธ์— ์ €์žฅํ•ด๋‘๋Š” ๋ฐฉ๋ฒ•์„ ๋– ์˜ฌ๋ ค,
์ƒ๊ธฐ ํ’€์ด์ฒ˜๋Ÿผ ํ•ด๊ฒฐํ•˜๋ ค๊ณ  ํ–ˆ์œผ๋‚˜ ๋˜ ๋‹ค์‹œ ์‹œ๊ฐ„์ดˆ๊ณผ์— ์ง๋ฉดํ–ˆ๋‹ค๐Ÿ˜ญ

๐Ÿ‘‰๐Ÿป์ˆ˜ํ•™์ ์œผ๋กœ ์ ‘๊ทผํ•˜์ž
n์„ ์ž…๋ ฅ๋ฐ›๋˜ a,b์— n//2๋ฅผ ๊ฐ๊ฐ ์ €์žฅํ•˜์—ฌ prime(a)์™€ prime(b)๊ฐ€ ๋‘˜ ๋‹ค ์†Œ์ˆ˜์ด๋ฉด ์ถœ๋ ฅ,
์†Œ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ฉด a--, b++ํ•˜๋ฉฐ ๋‹ค์‹œ ๋น„๊ตํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ’€์–ด๋ณด์ž
a,b ์ดˆ๊ธฐ๊ฐ’์„ n//2์œผ๋กœ ์ง€์ •ํ•œ ์ด์œ  = n์ด 10์ธ ๊ฒฝ์šฐ 5,5๊ฐ€ ์ œ์ผ ์ฐจ์ด๊ฐ€ ์ž‘์œผ๋‹ˆ๊นŒ


๐Ÿ“์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„์ˆœ์„œ

  1. ์†Œ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” prime()ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์ž
  2. t๋งŒํผ n์„ ์ž…๋ ฅ๋ฐ›์œผ๋ฉฐ a,b์— n//2๊ฐ’์„ ์ €์žฅํ•˜์—ฌ prime(a)์™€ prime(b)๊ฐ€ True์ด๋ฉด ์ถœ๋ ฅํ•˜์ž
    (ํ•˜๋‚˜๋ผ๋„ False์ด๋ฉด a++, b--)

๐Ÿ’ป๊ฒฐ๊ณผ์ฝ”๋“œ

import sys
input = sys.stdin.readline

t = int(input().rstrip())

def prime(n):
    for i in range(2,int(n**0.5)+1):
        if n % i == 0:
            return False
    return True

for _ in range(t):
    n = int(input().rstrip())
    a,b = n//2, n//2
    while a > 0:
        if prime(a) and prime(b): #๋‘˜ ๋‹ค True(์†Œ์ˆ˜)์ธ ๊ฒฝ์šฐ
            print(a, b)
            break
        else:
            a -= 1
            b += 1
โ“์ด ์ฝ”๋“œ๊ฐ€ ํšจ์œจ์ ์ธ ์ด์œ 

๐Ÿ‘‰๐Ÿป ๋ชจ๋“ ์ผ€์ด์Šค์˜ prime()์„ ์ €์žฅํ•˜๊ฒŒ ๋งŒ๋“œ๋Š”๊ฒŒ ๋ฌธ์ œ์˜ ํ•จ์ •์ธ๋ฐ, 
   ์œ„ ์ฝ”๋“œ๋Š” a,b๋งŒ prime()์„ ํ˜ธ์ถœํ•˜์—ฌ ๋‘˜ ๋‹ค True๋ฅผ ๋ฐ˜ํ™˜ํ•  ๋•Œ๋งŒ ์ถœ๋ ฅํ•œ๋‹ค.
   (๋ชจ๋“ ์ผ€์ด์Šค๋ฅผ prime()ํ•˜๋ฉด n๋ฒˆ / a,b๋งŒ prime()ํ•˜๋ฉด n/2๋ฒˆ๋งŒ ํ˜ธ์ถœํ•˜๊ฒŒ๋จ!)

โœ๏ธ1. prim() ํ•จ์ˆ˜ ๋งŒ๋“ค๊ธฐ (์†Œ์ˆ˜์ด๋ฉด True, ์•„๋‹ˆ๋ฉด Flase ๋ฐ˜ํ™˜)

import sys
input = sys.stdin.readline

t = int(input().rstrip())

def prime(n):
    for i in range(2,int(n**0.5)+1):
        if n % i == 0:
            return False
    return True

โœ๏ธ2. n//2๋ฅผ a,b์— ์ €์žฅํ•˜์—ฌ prime(a) and prime(b) โ†’ true์ด๋ฉด ์ถœ๋ ฅ

for _ in range(t):
    n = int(input().rstrip())
    a,b = n//2, n//2
    while a > 0:  #a๋ฅผ a--ํ•ด๊ฐ€๋ฉด ๊ฒฐ๊ตญ 0์— ๋„๋‹ฌํ•  ํ…Œ๋‹ˆ๊นŒ 1์ด ๋  ๋•Œ๊นŒ์ง€๋งŒ ์‹คํ–‰
        if prime(a) and prime(b): #a,b ๋‹ค True์ด๋ฉด ์ถœ๋ ฅ
            print(a, b)
            break
        else: #ํ•˜๋‚˜๋ผ๋„ False์ด๋ฉด a--, b++ํ›„ ๋‹ค์‹œ ๊ฒ€์‚ฌ
            a -= 1
            b += 1

๐Ÿ“š์ดˆ๊ธฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์˜ค๋ฅ˜์™€ ์ •๋ฆฌ

โ€ป์ดˆ๊ธฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜

โœ๐Ÿป N์˜ ์†Œ์ˆ˜๋ฅผ ๋‹ค ๊ตฌํ•˜์—ฌ ์ ˆ๋Œ“๊ฐ’์ด ์ตœ์†Œ๊ฐ’์ด๋ฉด ์ถœ๋ ฅํ•˜๊ธฐ

import sys
input = sys.stdin.readline

t = int(input().rstrip())

def prime(n):
    res = []
    res_prime = ''
    for num in range(n):
        err = 0
        for i in range(2,int(num**0.5)+1):
            if num%i == 0:
                err += 1
        if err == 0:
            res.append(num)
    cnt = max(res)-min(res)
    for a in res:
        for b in res:
            if a+b == n:
                if abs(a-b) < cnt:
                    cnt = abs(a-b)
                    res_prime = str(a)+' '+str(b)
                else:
                    pass
    return res_prime

for _ in range(t):
    n = int(input().rstrip())
    print(prime(n))
โ“๋‚ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ํ‹€๋ฆฐ ์ด์œ 

ํ‹€๋ฆฌ์ง€์•Š์•˜๋‹ค. ์ •๋‹ต์ด๋‹ค.
๊ทธ๋Ÿฌ๋‚˜ n์ด ์ž…๋ ฅ๋  ๋•Œ๋งˆ๋‹ค ๋ชจ๋“  ์†Œ์ˆ˜๋ฅผ ๊ตฌํ•˜์—ฌ list(res)์— ์ €์žฅํ•˜๊ณ ,
res๋ฅผ ๋‹ค์‹œ ์ ˆ๋Œ“๊ฐ’์„ ๋น„๊ตํ•˜๋ฉด์„œ prime()์•ˆ์—์„œ ๋ถˆํ•„์š”ํ•œ for/if๋ฌธ ๋™์ž‘์ด ๋งŽ๊ธฐ ๋•Œ๋ฌธ์—
์‹œ๊ฐ„์ดˆ๊ณผ๋ฅผ ๋ฐœ์ƒ์‹œ์ผฐ๋‹ค

  1. ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์„ค๊ณ„ ๊ณผ์ •์—์„œ O(N)๋ฅผ ์ƒ๊ฐํ•˜๋Š” ๊ฒƒ์€ ์ด์ œ ๋‹น์—ฐํ•˜๋‹ค
    '์ „์ฒด ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ• ์ง€ / ๋™์ž‘ํ•˜๋ฉด์„œ ๋ถˆํ•„์š”ํ•œ ๊ณ„์‚ฐ์€ ์—†๋Š”์ง€'
    ํ™•์ธํ•˜๋Š” ๊ณผ์ •์ด ๋งค์šฐ ์ค‘์š”ํ•˜๋‹ค

    ์–ด๋–ค ๊ฒฝ์šฐ๋Š” ์ „์ฒด ๊ณผ์ •์„ ๋‹ค ๊ณ„์‚ฐํ•˜์—ฌ ์ €์žฅํ•˜๊ณ 
    ๊ทธ๊ฒƒ์„ ๋ฝ‘์•„ ์“ฐ๋Š” ๊ฒƒ์ด ํšจ์œจ์ ์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๋˜๊ฒ ์ง€๋งŒ,
    
    ๋˜ ์–ด๋–ค ๊ฒฝ์šฐ๋Š” ์ „์ฒด ๊ณผ์ •์„ ๋‹ค ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค
    ํ•„์š”ํ•œ ๊ฒฝ์šฐ๋งŒ ๊ณ„์‚ฐํ•˜๋ฉฐ ๊ฒฐ๊ณผ๋ฅผ ๋„์ถœํ•ด๋‚ด๋Š” ๊ฒƒ์ด ํšจ์œจ์ ์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๋  ์ˆ˜ ์žˆ๋‹ค....
profile
keynene
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€