본문 바로가기
BOJ

[python] 백준 1484번 - 다이어트

by yujinkimkim 2023. 7. 25.

1484번: 다이어트 (acmicpc.net)

 

1484번: 다이어트

성원이는 다이어트를 시도중이다. 성원이는 정말 정말 무겁기 때문에, 저울이 부셔졌다. 성원이의 힘겨운 다이어트 시도를 보고만 있던 엔토피아는 성원이에게 새로운 저울을 선물해 주었다.

www.acmicpc.net

정답코드

# n^2 -x^2 = 15
# (n+x)(n-x) = 15
import math
import sys
input = sys.stdin.readline

n = int(input())
arr = []

check = True
for i in range(1,n):
    temp = math.pow(i,2) + n
    temp = math.sqrt(temp)
    isInt = float.is_integer(temp)
    if isInt:
        check = False
        print((int)(temp))
if check:
    print(-1)

밍죵이가 나 다이어트한다고 선물로 준 문제 ㅎㅎ

 

처음 시도는

# n^2 -x^2 = 15
# (n+x)(n-x) = 15
import sys
input = sys.stdin.readline

n = int(input())

ans = 1
for i in range(1,n):
    if ans == i:
        continue
    if n % (i + ans) == 0 and n % (i - ans) == 0:
        print(i," ",ans,"\n")
        ans+=1
    else:
        ans += 1

생각해보니까 그냥 약수면 다 출력하더라구요

오랜만에 부등식 끄적인 거 같아서 조았습니당

# n^2 -x^2 = 15
# (n+x)(n-x) = 15
import sys
input = sys.stdin.readline

n = int(input())

for i in range(1,n):
    for j in range(1, n - i + 1):
        if i == j :
            continue
        if (i + j)*(i - j) == n:
            print(i)

근데 시간초과나용

이것저것 쓰다가 어차피 이중포문 돌릴거 math. 얘네 쓰는 게 나을 거 같아서 해봤는데 성공했슴니다

import math
import sys
input = sys.stdin.readline

n = int(input())
arr = []

check = True
for i in range(1,n):
    temp = math.pow(i,2) + n
    temp = math.sqrt(temp)
    isInt = float.is_integer(temp)
    if isInt:
        check = False
        print((int)(temp))
if check:
    print(-1)

4^2 - 1^2 = 15

이런 구조니까

그냥 15 + 1^2로 해서 

위에 값 제곱근 중에 정수만 찾아서 출력했습니다

정수인지 확인하는거는

Python에서 변수가 정수인지 여부 확인 (techiedelight.com)

 

Python에서 변수가 정수인지 여부 확인

이 포스트에서는 Python에서 변수가 정수인지 아닌지 확인하는 방법에 대해 설명합니다. 1. 사용 isinstance() 기능 주어진 변수가 정수인지 확인하는 표준 솔루션은 isinstance() 기능. 그것은 반환 True

www.techiedelight.com

이 선생님 글 참고했슴니다

'BOJ' 카테고리의 다른 글

[python]백준 1756번 - 피자 굽기  (0) 2023.07.27
[python] 백준 1027번 - 고층 건물  (2) 2023.07.26
[python]백준 2240번 - 자두나무  (2) 2023.07.24
[python] 백준 1309번 - 동물원  (2) 2023.07.23
[python]백준 1261번 - 알고스팟  (2) 2023.07.22

댓글