Skip to content

Instantly share code, notes, and snippets.

@qkreltms
Last active March 15, 2018 14:35
Show Gist options
  • Select an option

  • Save qkreltms/62d62d3977cd57e5f4179e86fc7991a6 to your computer and use it in GitHub Desktop.

Select an option

Save qkreltms/62d62d3977cd57e5f4179e86fc7991a6 to your computer and use it in GitHub Desktop.
백준 1158번 조세퍼스 문제 (Circular Linked List Version), https://www.acmicpc.net/problem/1158
class Node:
def __init__(self, data):
self.data = data
self.next = None
class CircularLinkedList:
def __init__(self):
self.tail = None
self.head = None
self.cur = None
self.pre = None
self.node_num = 0
def append(self, data):
new_node = Node(data)
self.node_num += 1
if self.head is None:
self.head = new_node
self.tail = self.head
else:
self.tail.next = new_node
self.tail = new_node
self.tail.next = self.head
def pop(self, index):
if index == 1: # 만약 건너뛰는 범위가 1이라면
result = '<'
for i in range(1, user[0]+1):
if i == user[0]: # 마지막일 때 ', '을 쓰지 않고 '>'를 사용함
result = result + str(i) + '>'
else:
result = result + str(i) + ', '
return result
self.cur = self.head
result = "<"
ctn = 1
while self.pre is not self.cur:
if ctn % index == 0:
data = self.cur.data
self.pre.next = self.cur.next
self.cur = self.cur.next
result = result + str(data) + ', '
ctn += 1
self.pre = self.cur
self.cur = self.cur.next
ctn += 1
result = result + str(self.pre.data) + '>'
return result
user = list(map(int, input().split()))
cll = CircularLinkedList()
for i in range(user[0]): # 1부터 n 까지 원형 링크드 리스트를 만듦
cll.append(i+1)
print(cll.pop(user[1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment