Algorithm/Python

Algorithm/Python

[백준] 2448 별 찍기 - 11

https://www.acmicpc.net/problem/2448from collections import dequen = int(input())# 정답 모양을 만드려면 배열의 가로 길이를 2*n-1로 맞춰야 합니다.arr = [[' '] * (2 * n-1) for _ in range(n)] queue = deque()queue.append((0, n - 1, n)) # 시작 위치를 (0, n-1)로 설정while queue: y, x, l = queue.popleft() if l == 3: arr[y][x] = arr[y + 1][x - 1] = arr[y + 1][x + 1] = '*' for i in range(-2, 3): arr[y + ..

Algorithm/Python

[백준] 4344 평균은 넘겠지

https://www.acmicpc.net/problem/4344 c = int(input())for _ in range(c): arr = list(map(int, input().split())) avg = sum(arr[1:])/arr[0] print(f'{round(100*sum(map(lambda x: x>avg, arr[1:]))/arr[0],3)}%')

Algorithm/Python

[백준] 14503 로봇 청소기

https://www.acmicpc.net/problem/14503import sysinput = sys.stdin.readlinen,m = map(int, input().split())r, c, d = map(int, input().split()) # 로봇청소기 초기 위치, 방향mv = [[-1,0],[0,1],[1,0],[0,-1]]board = []for _ in range(n): board.append(list(map(int, input().split())))cnt=0while True: if board[r][c]==0: # 청소 필요 board[r][c]=-1 # 청소 완 cnt+=1 else: isMv=False for _ in..

Algorithm/Python

[백준] 1389 케빈 베이컨의 6단계 법칙

https://www.acmicpc.net/problem/1389import sysfrom collections import dequeinput=sys.stdin.readlinen, m = map(int, input().split())adj = [[n for _ in range(n+1)] for _ in range(n+1)]for i in range(1, n+1): adj[i][i] = 0for _ in range(m): a,b = map(int, input().split()) adj[a][b] = 1 adj[b][a] = 1for k in range(1, n+1): for i in range(1, n+1): for j in range(1, n+1): ..

Algorithm/Python

[백준] 1072 게임

https://www.acmicpc.net/problem/1072import sys, mathinput = sys.stdin.readlinex,y = map(int, input().split())z_start = math.floor(100*y/x)left, right=0, xans=-1if y==x: print(-1)else: while leftz_start: ans=mid right = mid-1 else: left=mid+1 print(ans)

Algorithm/Python

[백준] 1106 호텔

https://www.acmicpc.net/problem/1106 가장 적은 비용을 들여 고객의 수를 주어진 고객의 수 이상으로 확보해야 하기 때문에 0-1 Knapsack Problem의 변형 문제로 보인다! 차이점이라면 가방은 최대 무게 이하여야 한다는 거지만, 여기는 주어진 고객의 수 이상이어야 한다는 것이다. dp 배열의 인덱스는 모은 고객의 수이고, 안의 값에는 해당 고객을 모으기 위해 필요한 최소 비용이다.하나의 도시에서 모을 수 있는 비용 - 고객은 중복이 되기 때문에(배수) 인덱스는 작은 곳부터 c+100까지 구했다.import sysinput = sys.stdin.readlinec,n = map(int, input().split())dp = [0 for _ in range(1101)]f..

Algorithm/Python

[백준] 21317 징검다리 건너기

https://www.acmicpc.net/problem/21317import sysinput = sys.stdin.readlinen = int(input())if n == 1: print(0) sys.exit()arr = []for _ in range(n - 1): arr.append(list(map(int, input().split())))k = int(input())# dp[0][i]: 특수 점프를 사용하지 않고 i에 도달하는 최소 비용# dp[1][i]: 특수 점프를 사용하여 i에 도달하는 최소 비용dp = [[float('inf')] * n for _ in range(2)]dp[0][0] = 0# n=2, n=3 등의 작은 케이스를 명확하게 처리dp[0][1] = arr[0][0..

Algorithm/Python

[백준] 1735 분수

https://www.acmicpc.net/problem/1735 math 라이브러리의 gcd, lcm 함수를 이용해 최대공약수, 최소공배수를 구할 수 있다.gcd : 주어진 숫자들의 최대공약수lcm : 주어진 숫자들의 최소공배수 import matharr = []for _ in range(2): arr.append(list(map(int, input().split())))bunmo = math.lcm(arr[0][1], arr[1][1])bunja = arr[0][0]*(bunmo//arr[0][1])+arr[1][0]*(bunmo//arr[1][1])gcdvalue = math.gcd(bunmo, bunja)print(bunja//gcdvalue, bunmo//gcdvalue)

Algorithm/Python

[백준] 2512 예산

https://www.acmicpc.net/problem/2512n = int(input())budget = list(map(int, input().split()))total = int(input())left, right = 0, max(budget)ans=0while left

Algorithm/Python

[프로그래머스] 삼각 달팽이

https://school.programmers.co.kr/learn/courses/30/lessons/68645 해당 문제에 대해 좌표를 삼각형 모양을 따라가며, 값을 증가시키며 넣은 방식으로 먼저 구현해보았다. 그러나 다른 변에서는 괜찮지만, 매 삼각형의 시작 꼭짓점 부분은 위에서 이전 시작점보다 세 좌표 아래서 해야 하기 때문에 고민이 되었다. https://school.programmers.co.kr/questions/52719 해당 글을 참고하여, 움직이는 방향을 바꿔야 할 때 (즉, 다음 움직일 예정 좌표가 n이상 0 미만이거나 할당된 값이 0 초과인 경우) 방향을 바꾸는 식으로 작성하였다. 또한 삼각형의 값들을 채울 때에는 1~n-1길이의 배열들을 사용하였지만, 최종 답안은 하나의 배열이므로..

yoursin
'Algorithm/Python' 카테고리의 글 목록