본문 바로가기

코드포스

1971C Clock and Strings

C. Clock and Strings
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

There is a clock labeled with the numbers 11 through 1212 in clockwise order, as shown below.

In this example, (a,b,c,d)=(2,9,10,6)(𝑎,𝑏,𝑐,𝑑)=(2,9,10,6), and the strings intersect.

Alice and Bob have four distinct integers a𝑎, b𝑏, c𝑐, d𝑑 not more than 1212. Alice ties a red string connecting a𝑎 and b𝑏, and Bob ties a blue string connecting c𝑐 and d𝑑. Do the strings intersect? (The strings are straight line segments.)

Input

The first line contains a single integer t𝑡 (1t59401≤𝑡≤5940) — the number of test cases.

The only line of each test case contains four distinct integers a𝑎, b𝑏, c𝑐, d𝑑 (1a,b,c,d121≤𝑎,𝑏,𝑐,𝑑≤12).

Output

For each test case, output "YES" (without quotes) if the strings intersect, and "NO" (without quotes) otherwise.

You can output "YES" and "NO" in any case (for example, strings "yEs", "yes", and "Yes" will be recognized as a positive response).

 

 

Example
 
input
15
2 9 10 6
3 8 9 1
1 2 3 4
5 3 4 12
1 8 2 10
3 12 11 8
9 10 12 1
12 1 10 2
3 12 6 9
1 9 8 4
6 7 9 12
7 12 9 6
10 12 11 1
3 9 6 12
1 4 3 5
 
output
YES
NO
NO
YES
YES
NO
NO
NO
NO
NO
NO
YES
YES
YES
YES

 

정답 코드

# 사용자로부터 반복 횟수를 입력받음
repeat = int(input())

# 반복 횟수만큼 루프를 실행
for i in range(repeat):
    # 네 개의 정수를 입력받아 각각 a, b, c, d에 저장
    a, b, c, d = map(int, input().split())
    
    # 시계 숫자 배열 초기화 (1부터 12까지)
    clock = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
    
    # 주어진 범위 내의 숫자를 저장할 리스트 초기화
    list1 = []
    
    # a가 b보다 큰 경우 값을 서로 교환하여 a가 항상 b보다 작게 만듦
    if a > b:
        a, b = b, a
    
    # a부터 b-1까지의 숫자를 list1에 추가
    for j in range(a, b):
        list1.append(clock[j % 12])
    
    # a부터 b까지의 숫자를 clock에서 제거
    for k in range(a, b + 1):
        clock.remove(k % 12)
    
    # c와 d가 모두 clock에 있는 경우 'No' 출력
    if c in clock and d in clock:
        print('No')
    # c와 d가 모두 list1에 있는 경우 'No' 출력
    elif c in list1 and d in list1:
        print('No')
    
    # c가 clock에 있고 d가 list1에 있거나, 그 반대의 경우 'Yes' 출력
    if (c in clock and d in list1) or (c in list1 and d in clock):
        print('Yes')