백준파이썬

    [백준 9093번-C++/파이썬] 단어 뒤집기

    http://acmicpc.net/problem/9093 {코드} #include #include #include using namespace std; int main() { int n; string s; stack st; cin >> n; cin.ignore(); while (n--) { getline(cin, s); s += ' '; for (const char&c : s) { if (c == ' ') { while (!st.empty()) { cout

    [백준 19940번-파이썬/Python] 피자 오븐

    http://acmicpc.net/problem/19940 {코드}for _ in range(int(input())): n = int(input()) buttons = [0]*5 sixties, tens, ones = n//60, (n % 60)//10, n % 10 if ones > 5: tens += 1 ones -= 10 if tens > 3: sixties += 1 tens -= 6 if tens = 0)] = abs(tens) buttons[4-(ones >= 0)] = abs(ones) print(*buttons){설명}먼저 각 버튼을 누를 횟수를 저장할 ..

    [백준 19939번-파이썬/Python] 박 터뜨리기

    http://acmicpc.net/problem/19939 {코드} def solution(): n, k = map(int, input().split()) sum_minimum = k*(k+1)//2 if sum_minimum > n: return -1 if (n-sum_minimum) % k == 0: return k-1 else: return k print(solution()) {설명} K개의 바구니에 들어있는 공의 개수가 1개 이상이며 전부 달라야 하기 때문에 공의 최솟값은 k(k+1)/2이다. 그림처럼 각 바구니가 순서대로 공의 개수가 결정된다면 전의 바구니보다 1개 더 많을 때가 최소이기 때문이다. 그렇다면 최솟값보다 공의 개수가 적으면 -1을 반환하면 된다. 공의 개수가 더 많을 경우 1부터 ..

    [백준 11653번-파이썬/Python] 소인수분해

    http://acmicpc.net/problem/11653 {코드} n = int(input()) for i in range(2, n): if i*i > n: break while n%i == 0: print(i) n //= i if n > 1: print(n) {설명} 이 문제의 요점은 while문 안으로 들어가는 i가 무조건 소수라는 것이다. 그 이유는 어떤 수 n은 어떤 소수로 나누어지지 않을때까지 나누면 그 소수의 배수들로는 나누어지지 않기 때문이다. (예: n = 2^7 * 3^2 * 7*8이면 2로 나누면 2의 배수가 아니게 된다) 그리고 n의 약수는 루트n까지만 확인하면 되기 때문에 i*i가 n보다 크면 반복문을 나온다. 반복문을 마친 n이 1보다 크면 n이 소수이기 때문에 출력한다. (6일..

    [백준 10814번-파이썬/Python] 나이순 정렬

    http://acmicpc.net/problem/10814 {코드} data = [] for _ in range(int(input())): x, y = input().split() data.append((int(x), y)) data.sort(key=lambda x: x[0]) [print(*i) for i in data] {설명} 파이썬의 정렬은 가능한 한 기존의 인덱스를 유지하는 정렬, 안정된 정렬(Stable sort)을 수행합니다. 그렇기에 단순히 나이만 키값으로 주고 정렬하면 문제가 요구하는 답을 구할 수 있습니다.

    [백준 1181번-파이썬/Python] 단어 정렬

    http://acmicpc.net/problem/1181 {코드} import sys def input(): return sys.stdin.readline().rstrip() def print(val): sys.stdout.write(str(val)+'\n') data = sorted({input() for _ in range(int(input()))}, key=lambda s: (len(s), s)) [print(i) for i in data] {설명} 먼저 입력받은 원소들을 집합에 넣어 중복을 없애고 문자열의 길이을 기준으로 먼저 정렬하고 길이가 같으면 문자열을 기준으로 정렬하게 된다.

    [백준 11651번-파이썬/Python] 좌표 정렬하기 2

    http://acmicpc.net/problem/11651 {코드} import sys def input(): return sys.stdin.readline() def print(a, b): sys.stdout.write(f'{a} {b}\n') [print(*i) for i in sorted([list(map(int, input().split())) for _ in range(int(input()))], key=lambda x: (x[1], x[0]))] {설명} 이 문제는 11651의 좌표 정렬하기와 비슷한 문제로 입력량이 많아 sys모듈을 사용하고 뒤에 key를 통해 2번째 숫자를 기준으로 정렬하되 같은 값이 있다면 첫 번째 값이 더 작은 순으로 정렬한다.

    [백준 11650번-파이썬/Python] 좌표 정렬하기

    http://acmicpc.net/problem/11650 {코드} import sys [print(*x) for x in sorted([list(map(int, sys.stdin.readline().split())) for _ in range(int(sys.stdin.readline()))])] # --- # data = sorted([list(map(int, sys.stdin.readline().split())) for _ in range(int(sys.stdin.readline()))])] [print(*x) for x in data] {설명} 파이썬은 정렬할 때 다차원 배열의 경우 첫 번째 값부터 N번째 값까지 순차적으로 정렬합니다. 즉, 좌표와 같은 정렬에는 그냥 sorted만 사용하면 됩니다...

    [백준 1427번-파이썬/Python] 소트인사이드

    http://acmicpc.net/problem/1427 {코드} print(''.join(map(str, sorted(map(int, input()), reverse=True)))) # --- # sorted_string = sorted(map(int, input())) answer = ''.join(map(str, sorted_string)) print(answer) {설명} 주어진 문자열을 정수로 바꾸어 정렬한 다음 다시 문자열로 바꾼 다음 하나의 문자열로 합쳐 출력한다.

    [백준 2108번-파이썬/Python] 통계학

    http://acmicpc.net/problem/2108 {코드} import sys from collections import Counter def input(): return int(sys.stdin.readline()) def print(v): sys.stdout.write(str(v)+'\n') n = input() data = sorted([input() for _ in range(n)]) print(round(sum(data) / n)) # 평균 print(data[n // 2]) # 중앙값 # 최빈값 count = Counter(data).most_common() if len(count) > 1 and count[0][1] == count[1][1]: print(count[1][0]) els..