[백준/파이썬] 13298번 풀이
업데이트:
문제 정보
- 문제 출처: 백준 온라인 저지
- 문제 링크: 13298번 문제
- 문제풀이 코드 GitHub 링크
- 제출 언어: Python 3
풀이
문제
In the wonderful land of Robotopia, life is better than ever. This is thanks to all the robots, who work hard together. Different types of robots come in different forms, that is, different numbers of arms and different numbers of legs. By day, when they are working, robots are grouped so they can be assigned to different tasks. But by night, it is your job to count how many robots there are of each type within each group. Robots are naughty and energetic; they don’t like to stand still and it is difficult to count them. …
입력 요약
The first line contains an integer n (1 ≤ n ≤ 100). The following n lines each contain one test case. Each has six integers: l1 a1 l2 a2 lt at, where l1 and a1 are the number of legs and arms (respectively) for the first type of robot, l2 and a2 are those for the second type, and …
출력 요약
For each test case output two positive integers denoting the number of each of the two types of robots. Give first the count for the first type listed. If the test case has no solution or multiple solutions, output “?” (a question mark).
코드
import sys;read=sys.stdin.readline
for T in range(int(read())):
l=list(map(int,read().split()))
s=set([])
if l[0]!=l[1]:
for y in range(1,min(l[4]//l[2],l[5]//l[3])+1):
x=(l[4]-l[5]-(l[2]-l[3])*y)//(l[0]-l[1])
if x!=0!=y and x*l[0]+y*l[2]==l[4]and x*l[1]+y*l[3]==l[5]:s.add((x,y))
elif l[2]!=l[3]:
for x in range(1,min(l[4]//l[0],l[5]//l[1])+1):
y=(l[4]-l[5]-(l[0]-l[1])*x)//(l[2]-l[3])
if x!=0!=y and x*l[0]+y*l[2]==l[4]and x*l[1]+y*l[3]==l[5]:s.add((x,y))
else:
for y in range(1,l[4]//l[2]+1):
x=(l[4]-l[2]*y)//l[0]
if x!=0!=y and x*l[0]+y*l[2]==l[4]and x*l[1]+y*l[3]==l[5]:s.add((x,y))
print(*s.pop()if len(s)==1 else'?')
설명
핵심은 구현 관점에서 In the wonderful land of Robotopia, life is better than ever. This is thanks to all the robots, who work hard together. …를 만족하도록 로직을 구성하는 것입니다.
코드는 입력을 파싱한 뒤 조건 분기와 계산을 순서대로 수행하고, 문제에서 요구한 형식으로 결과를 출력합니다.
경계값과 예외 케이스도 함께 고려해 오답이 나기 쉬운 상황을 방지합니다.
댓글남기기