BeakJoon/Python

코드 # collections를 Counter로 쓸 수 있음 from collections import Counter # 심사위원 수와 표를 입력 v = int(input()) a = input() # 입력받은 표를 collections를 통해 요소 개수별로 정리한 내용을 mycount에 저장 mycount = Counter(a) # mycount에 저장된 내용을 바탕으로 개수를 비교하여 결과 출력 if mycount['A'] == mycount['B']: print('Tie') elif mycount['A'] > mycount['B']: print('A') else: print('B') 파이썬의 collections함수를 호출하여 만들었다 collections 함수는 문자열을 딕셔너리로 만들어서 딕셔너..
코드 t = int(input()) result = [] for _ in range(t): total = 0 r, e, c = map(int, input().split()) total = r + c if e total: result.append('advertise') for i in range(len(result)): print(result[i]) total을 for문 안에 선언해서 한 루프를 돌 때마다 0으로 초기화 시켜서 r과 c의 합을 e와 비교하게끔 만듬 result라는 배열을 선언하고 그 배열에 결과 문자열을 저장하..
걸림돌 입력받은 괄호를 조건에 맞게 구별하는 기능을 구현하는 것이 어렵다 코드 bowl = [] bowl = list(str(input())) result = 0 for i in range (len(bowl)): if i == 0: result += 10 elif bowl[i] == bowl[i-1]: result += 5 else: result += 10 print(result) bowl이라는 배열을 선언 이후 입력할 괄호들을 문자열 형태로 list에 저장하고 list의 길이만큼 반복하는 for문을 통해서 bowl list의 0번 인덱스 부터 마지막 인덱스 까지 괄호를 구별하여 result 값에 5,10 둘 중 하나를 더하게끔 만듬
코드 T = int(input()) result = [] # 결과를 저장 할 배열 선언 for _ in range(T): str_input = input().split() # 식을 문자열로 입력받음 for i in range(len(str_input)): if str_input[i] == '@': # '@'일 경우 str_input[0]에 '* 3' 을 해줌 str_input[0] = float(str_input[0]) * 3 elif str_input[i] == '%': # '%'일 경우 str_input[0]에 '+ 5' 을 해줌 str_input[0] = float(str_input[0]) + 5 elif str_input[i] == '#': # '#'일 경우 str_input[0]에 '- 7' 을..
코드 h,m,s = map(int,input().split()) sec = int(input()) s1 = (s+sec)%60 #최종 초 m1 = (s+sec)//60 m2 = (m+m1)%60 # 최종 분 h1 = (m+m1)//60 h2 = (h+h1)%24 # 최종 시각 print(h2,m2,s1)
코드 N = int(input()) tester = list(map(int, input().split())) viewer, sub_viewer = map(int, input().split()) result = N # 필요한 감독관 수를 저장 할 변수 선언 for i in range(N): tester[i] -= viewer if tester[i] > 0: if tester[i]%sub_viewer > 0: result += tester[i]//sub_viewer + 1 else: result += tester[i]//sub_viewer print(result) 결국 구해야 하는 것은 필요한 감독관의 수이기 때문에 총감독관 수를 먼저 뺀 값에서 부감독관이 맡을 수 있는 수로 나눈 값이 필요한 감독관의 수가..
코드 grade_dict = {'A+': 4.5, 'A0': 4.0, 'B+': 3.5, 'B0': 3.0, 'C+': 2.5, 'C0': 2.0, 'D+': 1.5, 'D0': 1.0, 'F': 0} total = 0 # 학점 총합을 담을 변수 result = 0 # (학점 * 과목평점) 총합을 담을 변수 for _ in range(20): name, credit, mark = input().split() credit = float(credit) if mark != 'P': # 등급이 P인 과목은 계산 안함 total += credit result += credit * grade_dict[mark] print('%.6f' % (result / total)) 딕셔너리를 사용하여 학점을 정의해주었고, if..
즉 감염당한 컴퓨터를 제외한 감염당한 컴퓨터들 개수를 출력하는 코드를 짜면 됨. 걸림돌 위 문제를 풀기위해서 배열이나 리스트를 이용하여 풀어보려고 했으나 너무 복잡해서 실패함 너비 우선 탐색( BFS(Breadth Frist Search) )를 이용하여 쉽게 해결함 코드 computer_num = int(input()) graph = [[] for _ in range(computer_num + 1)] pair_num = int(input()) for _ in range(pair_num): x, y = map(int, input().split()) graph[x].append(y) graph[y].append(x) # 너비 우선 탐색 알고리즘을 `dfs`함수로 만듬 def dfs(graph, s): vis..
쿼딩~
'BeakJoon/Python' 카테고리의 글 목록