백준 15649 - N과 M
·
Algorithm/백준
python의 permutation내장함수를 통해 풀 수 있는 문제 itertools.permutations()는 순열을 구해주고 itertools.combinations()는 조합을 구해준다. from itertools import permutations n, m = map(int,input().split()) number = permutations(range(1,n+1),m) for i in number: print(*i) www.acmicpc.net/problem/15649 15649번: N과 M (1) 한 줄에 하나씩 문제의 조건을 만족하는 수열을 출력한다. 중복되는 수열을 여러 번 출력하면 안되며, 각 수열은 공백으로 구분해서 출력해야 한다. 수열은 사전 순으로 증가하는 순서로 출력해 www.a..
백준 10773 - 제로 python
·
Algorithm/백준
스택을 활용하는 기본 문제이다. import sys input = sys.stdin.readline K = int(input()) stack = [] for i in range(K): num = int(input()) if num == 0: stack.pop(-1) else: stack.append(num) print(sum(stack)) www.acmicpc.net/problem/10773 10773번: 제로 첫 번째 줄에 정수 K가 주어진다. (1 ≤ K ≤ 100,000) 이후 K개의 줄에 정수가 1개씩 주어진다. 정수는 0에서 1,000,000 사이의 값을 가지며, 정수가 "0" 일 경우에는 가장 최근에 쓴 수를 지우고, 아닐 경 www.acmicpc.net
백준 10828 - 스택 python
·
Algorithm/백준
파이썬으로 스택의 기능을 구현하는 문제이다. import sys input = sys.stdin.readline stack =[] N = int(input()) for i in range(N): order = input().split() if order[0] == 'push': stack.append(order[1]) if order[0] == 'pop': if len(stack) == 0: print(-1) else: print(stack.pop(-1)) if order[0] == 'size': print(len(stack)) if order[0] == 'empty': if len(stack) == 0: print(1) else: print(0) if order[0] == 'top': if len(st..
백준 4344 - 평균은 넘겠지 파이썬
·
Algorithm/백준
import sys input = sys.stdin.readline T = int(input()) for _ in range(T): case = list(map(int, input().split())) avg_score = sum(case[1:]) / case[0] cnt = 0 for score in case[1:]: if score > avg_score: cnt += 1 # n번째까지만 표현하고 반올림하고 싶을때 사용할 수 있는 round 내장함수 print("%.3f" % round((cnt/case[0]) * 100, 3) + '%') # print(f'{class_avg:.3f}') python 3.6 버전부터 나온 fstring 문자열 포매팅을 활용해서도 답을 출력할 수 있다. www.acmi..
백준 2577- 숫자의 개수 python
·
Algorithm/백준
python count내장함수를 이용하면 해당 문자의 개수를 구할 수 있다. import sys input = sys.stdin.readline A = int(input()) B = int(input()) C = int(input()) result = A * B * C result = list(str(result)) for i in range(10): print(result.count(str(i))) www.acmicpc.net/problem/2577 2577번: 숫자의 개수 첫째 줄에 A, 둘째 줄에 B, 셋째 줄에 C가 주어진다. A, B, C는 모두 100보다 같거나 크고, 1,000보다 작은 자연수이다. www.acmicpc.net
백준 10817- 세 수
·
Algorithm/백준
sorted 내장 함수를 사용하면 쉽게 풀린다. import sys input = sys.stdin.readline lists = list(map(int, input().split())) lists = sorted(lists, reverse=True) print(lists[1]) www.acmicpc.net/problem/10817 10817번: 세 수 첫째 줄에 세 정수 A, B, C가 공백으로 구분되어 주어진다. (1 ≤ A, B, C ≤ 100) www.acmicpc.net
백준 10818 - 최소, 최대 python (구현)
·
Algorithm/백준
for문을 돌리지 않고 파이썬 내장함수로 값을 출력하는 방법 min - 반복 가능한 객체의 가장 작은 요소 값을 리턴한다. max - 반복 가능한 객체의 가장 큰 요소 값을 리턴한다. import sys input = sys.stdin.readline N = int(input()) lists = list(map(int, input().split())) # for문을 돌리지 않고 파이썬 내장함수로 값을 출력하는 방법 # min - 반복 가능한 객체의 가장 작은 요소 값을 리턴한다. # max - 반복 가능한 객체의 가장 큰 요소 값을 리턴한다. # print('{} {}'.format(min(lists),max(lists))) # 반복문을 활용해서 최대값, 최소값 구하는 방법 max, min = lists..
백준 10699 - 파이썬
·
Algorithm/백준
파이썬 datetime 모듈을 활용하여 풀었다. from datetime import datetime print(str(datetime.now())[:10]) docs.python.org/ko/3/library/datetime.html datetime — 기본 날짜와 시간 형 — Python 3.9.2 문서 datetime — 기본 날짜와 시간 형 소스 코드: Lib/datetime.py datetime 모듈은 날짜와 시간을 조작하는 클래스를 제공합니다. 날짜와 시간 산술이 지원되지만, 구현의 초점은 출력 포매팅과 조작을 위한 docs.python.org www.acmicpc.net/problem/10699 10699번: 오늘 날짜 서울의 오늘 날짜를 출력하는 프로그램을 작성하시오. www.acmicpc..
백준 15552 - 빠른 A + B
·
Algorithm/백준
입력받아야하는 문제일 경우라면 import sys input = sys.stdin.readline 이렇게 두 줄 먼저쓰고 input함수를 받는다. 다만 일반적인 파이썬 내장 input()함수와는 다른 점이라면 뒤에 개행문자가 추가된다는 차이가 있다. 개행문자를 제거해주기 위해서는 .rstrip()을 사용하면 된다. import sys input = sys.stdin.readline T = int(input()) for i in range(T): A, B = map(int,input().split()) print(A + B) www.acmicpc.net/problem/15552 15552번: 빠른 A+B 첫 줄에 테스트케이스의 개수 T가 주어진다. T는 최대 1,000,000이다. 다음 T줄에는 각각 두 ..
takoyummy
'Algorithm' 카테고리의 글 목록 (4 Page)