촬리의늘솔길

[BOJ/1244] 구현 문제 본문

✍️2023/Algorithm

[BOJ/1244] 구현 문제

리촬리 2023. 4. 26. 18:58

https://wikidocs.net/22805

 

1) 리스트 컴프리헨션

## 리스트 생성하기 기존에 배운 문법으로 1부터 10까지 정수를 순서대로 가지고 있는 리스트를 생성하는코드는 다음과 같습니다. ``` numbers = [] for n i…

wikidocs.net

리스트 컴프리헨션..편한데 어렵당..

https://chpark5667.tistory.com/58

 

2.(2) 코테용 데이터 입력 스킬 (input().split(), map() 등)

1. input() str = input() num = int(input()) 기본적인 입력 방법으로, input()자체는 문자열을 입력받는 것으로 처리됩니다. 그렇기 때문에 int(input())을 해주어야 숫자형 입력이 가능해집니다. num변수에 저장

chpark5667.tistory.com

진즉볼걸

 

배열

- 1차원 배열

 a = [ 값 for _ in range(N)] 

- 2차원 배열

m,n = 5,4

a = [[0]*m for _ in range(n)]

[0]*m 에 대해 for 문을 n 번반복함

 

for문을 이용한 N번 입력

answer = [int(input()) for _ in range(N)]

N은 사용자가 임의로 지정할 수 있습니다.
N이 5이면, 원소가 다섯개인 리스트를 생성할 수 있습니다.


내가 짠 개떡같은코드

(여전히 index bound,시간복잡도, 20개씩 끊어 출력하는거 그런것들 ^^ 생각못함)

N = int(input())
switch = [int(N) for _ in input().split()]
num = int(input())
student = [list(map(int,input().split())) for _ in range(num)]

for x, y in student:
  if x == 1:
    for i in range(len(switch)):
      if switch[y * i] == 0:
        switch[y * i] = 1
      else:
        switch[y * i] = 0
  else:
    for i in range(len(switch)):
      if switch[y - i] == switch[y + i]:
        if switch[y - i] == 0:
          switch[y - i] = switch[y + i] = 1
        else:
          switch[y - i] = switch[y + i] = 0
      else:
        if switch[y] == 0:
          switch[y] = 1
        else:
          switch[y] = 0

 

그래서 여러 알고리즘 참고해서

N = int(input())
switch = [0] + list(map(int, input().split()))
for _ in range(int(input())):
  sex, idx = map(int, input().split())
  i = 1
  if sex == 1:
    while idx * i <= N:
      switch[idx * i] = int(not switch[idx * i])
      i += 1
  elif sex == 2:
    switch[idx] = int(not switch[idx])
    while 1 <= idx - i and idx + 1 <= N and switch[idx - i] == switch[idx + 1]:
      switch[idx - i] = int(not switch[idx - i])
      switch[idx + i] = int(not switch[idx + i])
      i += 1

for i in range(1, N + 1):
  print(switch[i], end=" ")
  if i % 20 == 0:
    print()

이러헥 바꿨는데 런타임에러났다..

이번엔 뭐가문제냐..

 

흠...........

사실 여기서 더 다른점은

change 함수를 만들지않았다는점?

그거 말고는 잘 모르겠다.......해결.......실패.......

 

 

참고

https://corin-e.tistory.com/entry/%EB%B0%B1%EC%A4%80-1244-%EC%8A%A4%EC%9C%84%EC%B9%98-%EC%BC%9C%EA%B3%A0-%EB%81%84%EA%B8%B0-%ED%8C%8C%EC%9D%B4%EC%8D%AC

 

백준 1244 스위치 켜고 끄기 - 파이썬

https://www.acmicpc.net/problem/1244 1244번: 스위치 켜고 끄기 첫째 줄에는 스위치 개수가 주어진다. 스위치 개수는 100 이하인 양의 정수이다. 둘째 줄에는 각 스위치의 상태가 주어진다. 켜져 있으면 1,

corin-e.tistory.com

 

728x90

'✍️2023 > Algorithm' 카테고리의 다른 글

[프로그래머스] - 정렬 : K번째 수  (0) 2023.09.13
[이코테] DFS/BFS  (0) 2023.07.05
[BOJ/20053] 구현 기초문제  (0) 2023.04.23
[BOJ/2891]번 그리디 문제  (0) 2023.04.23
[프로그래머스/그리디] - 체육복  (0) 2023.04.22