파이썬
이전에 이어서 시뮬레이션 문제를 익혀보려 한다.
브3 공
맞은 코드
|
1
2
3
4
5
6
7
8
|
import sys
input = sys.stdin.readline
M = int(input())
ball = [0, 1, 0, 0]
for i in range(M):
X, Y = map(int, input().split())
ball[X], ball[Y] = ball[Y], ball[X]
print(ball.index(1))
|
cs |
파이썬에는 다른 언어와 다르게 swap 기능이란 것이 있어서 두 값을 서로 바꾸기 위해서 임시 변수 temp를 사용하지 않아도 된다.
a, b = b, a
이렇게 사용한다.
남의 코드
|
1
2
3
4
5
|
L = [x for x in range(4)]
for _ in ' '*int(input()):
a,b = map(int,input().split())
L[a],L[b] = L[b],L[a]
print(L.index(1))
|
cs |
또한 파이썬에는 List Comprehension(리스트 컴프리헨션)이란 것이 있어서 한 줄로 for 문과 if 문을 처리할 수 있다.
만약 List Comprehension을 사용하지 않았다면 for문 밑에 if문 밑에 실행문에서 들여쓰기를 두 번 해야한다.
|
1
2
3
4
5
|
mylist = [3, 2, 6, 7]
answer = []
for number in mylist:
if number % 2 == 0:
answer.append(number**2) # 들여쓰기를 두 번 함
|
cs |
이 코드를
|
1
2
|
mylist = [3, 2, 6, 7]
answer = [number**2 for number in mylist if number % 2 == 0]
|
cs |
이렇게 줄여서 사용할 수 있다.
단순한 반복문 뿐만 아니라 조건문을 같이 사용하거나 여러 개의 반복문, 조건문도 같이 사용가능하다.
아래의 블로그에 설명이 잘 되어 있다.
남의 코드 2
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
m=int(input())
a1,b1,c1=1,0,0
for i in range(m):
x,y=map(int,input().split())
if a1==1:
if x==1 and y==2:
a1-=1
b1+=1
elif x==1 and y==3:
a1-=1
c1+=1
elif x==2 and y==1:
a1-=1
b1+=1
elif x==2 and y==3:
continue
elif x==3 and y==1:
a1-=1
c1+=1
elif x==3 and y==2:
continue
elif b1==1:
if x==1 and y==2:
a1+=1
b1-=1
elif x==1 and y==3:
continue
elif x==2 and y==1:
a1+=1
b1-=1
elif x==2 and y==3:
b1-=1
c1+=1
elif x==3 and y==1:
continue
elif x==3 and y==2:
b1-=1
c1+=1
elif c1==1:
if x==1 and y==2:
continue
elif x==1 and y==3:
a1+=1
c1-=1
elif x==2 and y==1:
continue
elif x==2 and y==3:
b1+=1
c1-=1
elif x==3 and y==1:
a1+=1
c1-=1
elif x==3 and y==2:
c1-=1
b1+=1
if a1==1:
print(1)
elif b1==1:
print(2)
elif c1==1:
print(3)
else:
print(-1)
|
cs |
이 코드가 진짜 시뮬레이션처럼 하나하나 구현한 코드같다.
브3 창영마을
맞은 코드
|
1
2
3
4
5
6
7
8
9
10
|
import sys
shufflingWay = sys.stdin.readline().rstrip()
ball = [0, 1, 0, 0]
for i in shufflingWay:
previous = ord(i)-ord('A')+1
present = previous + 1
if present > 3:
present -= 3
ball[previous], ball[present] = ball[present], ball[previous]
print(ball.index(1))
|
cs |
남의 코드
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
a = input()
x = 'x'
y = 'y'
z = 'z'
for i in a:
if i=='A':
x,y=y,x
elif i=='B':
y,z=z,y
else:
x,z=z,x
if x=='x':
print(1)
elif y=='x':
print(2)
else:
print(3)
|
cs |
처음에 이 코드처럼 짜려고 했으나 이렇게 짰을 경우, 3가지가 아니라 차후에 컵을 늘렸을 때 해줘야 할 작업이 많을 것 같아서 확장성을 고려해서 위 코드처럼 짰다.
sys.stdin.readline()으로 입력을 받을 경우, 입력이 빠르긴 하지만 젤 마지막에 \n이 무조건 붙는다.
따라서 rstrip()으로 제일 오른쪽 공백을 제거해주고 사용한다.
ord() 함수로 아스키 코드를 이용하여 컵의 인덱스를 구해줬고 swap으로 구현했다.
백준 시뮬레이션 태그의 기초 문제들을 풀어봤는데 뭔가 내가 생각하던 시뮬레이션 문제들이 아니다.
다음부턴 문제의 조건을 그대로 구현하는 이런 식의 문제말고 격자 시뮬레이션 문제들만 골라서 풀어봐야겠다.
'Python' 카테고리의 다른 글
| 211202 파이썬 코딩테스트 수업 2일차 (0) | 2021.12.02 |
|---|---|
| 211201 파이썬 코딩테스트 수업 1일차 (0) | 2021.12.01 |
| 211127 백준 코딩연습 파이썬 14470 전자레인지 (0) | 2021.11.27 |
| 211126 백준 코딩연습 파이썬 재귀 함수 10829 이진수 변환 (0) | 2021.11.26 |
| 211124 백준 코딩연습 파이썬 그리디 알고리즘 11399 ATM, 11047 동전 0, 1931 회의실 배정 (0) | 2021.11.24 |




최근댓글