풀이 : 체스판의 크기는 8x8로 고정되어 있고, 체스판의 검정색과 하얀색이 번갈아 가면서 칠해져 있습니다.
체스판
리스트의 index시작은 0부터 시작하므로
1차원 list index가 0,2,4,6,8인 배열은, 2차원 index가 짝수인 것이 하얀색 칸이고,
1차원 list index가 1,3,5,7인 배열은, 2차원 index가 홀수인 것이 하얀색 칸입니다.
그래서 반복문을 통해 하얀 칸 위에 말이 몇 개 있는지 세어 줍니다.
chess_board = []
count = 0
for _ in range(8):
chess_board.append(list(input()))
for i in range(0,8):
if(i%2 == 0):
for j in range(0,8,2):
if(chess_board[i][j] == 'F'):
count += 1
else:
for j in range(1,8,2):
if(chess_board[i][j] == 'F'):
count += 1
print(count)
그래서, b만큼의 반복문으로 숫자가 언제 반복되는지 확인하고, 순환되는 리스트를 만듭니다.
그리고 리스트의 첫 index는 시작이 0이므로, 구한 값에서 1을 빼주어 처리될 컴퓨터의 번호를 구합니다.
t = int(input())
for _ in range(t):
a,b = map(int,input().split())
#숫자가 언제 반복되는지 확인합니다.
l = [a%10]
x = a
for _ in range(b):
x = (x*a) % 10
if (l[0] == x):
break
else:
l.append(x)
y = b%len(l) -1
if(l[y]==0):
print(10)
else:
print(l[y])
import sys
input = sys.stdin.readline
#Root node 찾기
def find(x):
if x== parent[x]:
return x
else:
root_x = find(parent[x])
parent[x] = root_x
return parent[x]
def union(x,y):
root_x = find(x)
root_y = find(y)
if root_x != root_y:
parent[root_y] = root_x
number[root_x] += number[root_y]
T=int(input())
for _ in range(T):
parent = dict()
number = dict()
r = int(input())
for _ in range(r):
x,y = input().split()
if x not in parent:
parent[x]=x
number[x]=1
if y not in parent:
parent[y]=y
number[y]=1
union(x,y)
print(number[find(x)])
문제에서 생각해봐야할 것은 '스위치를 중심으로 좌우가 대칭이면서 가장 많은 스위치를 포함하는 구간을 찾아서, 그 구간에 속한 스위치의 상태를 모두 바꾼다.' 는 것이다.
스위치의 길이를 반으로 나눈 후, 그 길이만큼 다 비교하는 방법을 택했다.
N=int(input())
btn=[-1] + list(map(int,input().split()))
student_num = int(input())
def switch(num):
if(btn[num]==0):
btn[num]=1
else:
btn[num]=0
return
for _ in range(student_num):
sex, num = map(int,input().split())
if(sex==1):
for i in range(num, N+1, num):
switch(i)
else:
switch(num)
for j in range(N//2):
if num+j > N or num-j < 1: break
if btn[num+j] == btn[num-j]:
switch(num+j)
switch(num-j)
else:
break
for i in range(1,N+1):
print(btn[i], end=' ')
if i%20 == 0: print()