[백준 1193 파이썬] - 분수찾기

zsunny·2022년 6월 27일
0

📌 문제

💯 정답

n = int(input())
line = 1

while n > line:
    n -= line
    line += 1

if line % 2 == 0:
    up = n
    down = line - n + 1
else:
    up = line - n + 1
    down = n

print(up,'/',down, sep="")

📝 설명

• line 1 = 1/1
  line 2 = 1/2, 2/1
  line 3 = 3/1, 2/2, 1/3
  line 4 = 1/4, 2/3, 3/2, 4/1
• n -= line을 하면 각 line에서 n이 몇번째에 위치하는지 알 수 있다.
• line이 짝수일때와 홀수일때 분모 분자의 증감 양상이 다르다.
• 짝수일때는 분모 -1씩 분자 +1씩, 홀수일때는 분모 +1씩 분자 -1씩 증감한다.
• 출력시 sep=""를 써서 구분자를 변경해준다.

⭐️ 알고가기 _ sep=" "

🔎 sep (separation)

  • sep는 구분자로 print문에서 사용된다.
  • 기본은 공백이다.
s1 = hello
s2 = world

print(s1, s2)				// hello world
print(s1, s2, sep="")		// helloworld
print(s1, s2, sep="_")		// hello_world
profile
매일 성장하는 예비 웹 개발자 🌱

0개의 댓글