728x90

 

파이썬

 

이전에 이어서 시뮬레이션 문제를 익혀보려 한다.

 

브3 공

 

맞은 코드

 

1
2
3
4
5
6
7
8
import sys
input = sys.stdin.readline
= int(input())
ball = [0100]
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
= [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 = [3267]
answer = []
for number in mylist:
    if number % 2 == 0:
        answer.append(number**2# 들여쓰기를 두 번 함
cs

 

 

이 코드를

 

1
2
mylist = [3267]
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 = [0100]
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
= input()
= 'x'
= 'y'
= '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으로 구현했다.

 

백준 시뮬레이션 태그의 기초 문제들을 풀어봤는데 뭔가 내가 생각하던 시뮬레이션 문제들이 아니다.

다음부턴 문제의 조건을 그대로 구현하는 이런 식의 문제말고 격자 시뮬레이션 문제들만 골라서 풀어봐야겠다.

 

728x90
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기