반응형

""" [백준] 1019번 책 페이지 - PYTHON
    https://www.acmicpc.net/problem/1019
    문제
    지민이는 전체 페이지의 수가 N인 책이 하나 있다. 첫 페이지는 1 페이지이고, 마지막 페이지는 N 페이지이다. 각 숫자가 전체 페이지 번호에서 모두 몇 번 나오는지 구해보자.

    입력
    첫째 줄에 N이 주어진다. N은 1,000,000,000보다 작거나 같은 자연수이다.

    출력
    첫째 줄에 0이 총 몇 번 나오는지, 1이 총 몇 번 나오는지, ..., 9가 총 몇 번 나오는지를 공백으로 구분해 출력한다.

    예제 입력 1 
    11
    예제 출력 1 
    1 4 1 1 1 1 1 1 1 1
    예제 입력 2 
    7
    예제 출력 2 
    0 1 1 1 1 1 1 1 0 0
"""

import sys

n=int(sys.stdin.readline().strip())
a=[0]*10
b=1
while n != 0:
    while n % 10 != 9:
        for i in str(n):
            a[int(i)] += b
        n -= 1
        
    if n < 10:
        for k in range(n+1):
            a[k] += b
        a[0] -= b
        break
    
    else:
        for i in range(10):
            a[i] += (n//10 + 1) * b
    a[0] -= b
    b *= 10
    n //= 10
    
for i in a:
    print(i,end=' ')
반응형
반응형

[백준] 1018번 체스판 다시 칠하기 - python

 

https://www.acmicpc.net/problem/1018

 

1018번: 체스판 다시 칠하기

첫째 줄에 N과 M이 주어진다. N과 M은 8보다 크거나 같고, 50보다 작거나 같은 자연수이다. 둘째 줄부터 N개의 줄에는 보드의 각 행의 상태가 주어진다. B는 검은색이며, W는 흰색이다.

www.acmicpc.net

예제 입력 1 복사

8 8
WBWBWBWB
BWBWBWBW
WBWBWBWB
BWBBBWBW
WBWBWBWB
BWBWBWBW
WBWBWBWB
BWBWBWBW

예제 출력 1 복사

1

예제 입력 2 복사

10 13
BBBBBBBBWBWBW
BBBBBBBBBWBWB
BBBBBBBBWBWBW
BBBBBBBBBWBWB
BBBBBBBBWBWBW
BBBBBBBBBWBWB
BBBBBBBBWBWBW
BBBBBBBBBWBWB
WWWWWWWWWWBWB
WWWWWWWWWWBWB

예제 출력 2 복사

12

예제 입력 3 복사

8 8
BWBWBWBW
WBWBWBWB
BWBWBWBW
WBWBWBWB
BWBWBWBW
WBWBWBWB
BWBWBWBW
WBWBWBWB

예제 출력 3 복사

0

예제 입력 4 복사

9 23
BBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBW

예제 출력 4 복사

31

예제 입력 5 복사

10 10
BBBBBBBBBB
BBWBWBWBWB
BWBWBWBWBB
BBWBWBWBWB
BWBWBWBWBB
BBWBWBWBWB
BWBWBWBWBB
BBWBWBWBWB
BWBWBWBWBB
BBBBBBBBBB

예제 출력 5 복사

0

예제 입력 6 복사

8 8
WBWBWBWB
BWBWBWBW
WBWBWBWB
BWBBBWBW
WBWBWBWB
BWBWBWBW
WBWBWWWB
BWBWBWBW

예제 출력 6 복사

2

예제 입력 7 복사

11 12
BWWBWWBWWBWW
BWWBWBBWWBWW
WBWWBWBBWWBW
BWWBWBBWWBWW
WBWWBWBBWWBW
BWWBWBBWWBWW
WBWWBWBBWWBW
BWWBWBWWWBWW
WBWWBWBBWWBW
BWWBWBBWWBWW
WBWWBWBBWWBW

예제 출력 7 복사

15
import sys

N, M = map(int, sys.stdin.readline().split())

board = []
white_first = []
black_first = []

for _ in range(N):
    row = sys.stdin.readline().replace("\n", "")
    board.append([i for i in row])

initial_color = board[0][0]

# 흰색으로 시작하는 체스판을 만들 경우
for index, row in enumerate(board):
    painting = []
    if index % 2 == 0: current_color = "W"
    else: current_color = "B"

    for value in row:
        if value == current_color: painting.append(0)
        else: painting.append(1)
        
        if current_color == "W": current_color = "B"
        else: current_color = "W"
    white_first.append(painting)

# 검은색으로 시작하는 체스판을 만들 경우
for index, row in enumerate(board):
    painting = []
    if index % 2 == 0: current_color = "B"
    else: current_color = "W"

    for value in row:
        if value == current_color: painting.append(0)
        else: painting.append(1)
        
        if current_color == "W": current_color = "B"
        else: current_color = "W"
    black_first.append(painting)

# 최솟값을 초기화 할 때, 보드의 최대 크기인 50*50 = 2500으로 한다.
min_count = 2500
for i in range(N-8+1):
    rows = white_first[i:i+8]
    for j in range(M-8+1):
        paint = 0
        for row in rows:
            paint += sum(row[j:j+8])
        if paint < min_count: min_count = paint

for i in range(N-8+1):
    rows = black_first[i:i+8]
    for j in range(M-8+1):
        paint = 0
        for row in rows:
            paint += sum(row[j:j+8])
        if paint < min_count: min_count = paint

print(min_count)

# 참조 : https://nerogarret.tistory.com/35

반응형
반응형

[python] 구문 퀴즈, 파이썬

""" 
# What will be the output?
    A.Infinite loop
    B.120
    C.47
    D.48
"""
import secrets


def rec(a,b):
    if a == 0:
        return b
    else:
        return rec(a - 1, a + b)

print(rec(8, 12))

# 48 


s1 = [92,57,[82,75]]
s2 = s1[:]
s2[2][1] = 23
print(s1)

# 클래스 
result = 0

def add(num):
    global result
    result += num
    return result

print(add(3))
print(add(4))


"""
    # what will be the output?
    A.@corporate
    B.qna
    C.corporate
    D.@qna
"""
s = ['@corporate','qna']
print(s[:-1][-1])
반응형
반응형

[python] 구문 퀴즈, 파이썬

""" 
# What will be the output?
    A.Infinite loop
    B.120
    C.47
    D.48
"""
def rec(a,b):
    if a == 0:
        return b
    else:
        return rec(a - 1, a + b)

print(rec(8, 12))

# 48

 

반응형
반응형

[백준] 1017번 소수 쌍 - PYTHON

https://www.acmicpc.net/problem/1017

 

1017번: 소수 쌍

지민이는 수의 리스트가 있을 때, 이를 짝지어 각 쌍의 합이 소수가 되게 하려고 한다. 예를 들어, {1, 4, 7, 10, 11, 12}가 있다고 하자. 지민이는 다음과 같이 짝지을 수 있다. 1 + 4 = 5, 7 + 10 = 17, 11 +

www.acmicpc.net

import sys
import math

def dfs(x):
    global Y
    global matched
    global visited
    if visited[Y.index(x)]: return False
    visited[Y.index(x)] = True
    for y in Y:
        if x + y in primes:
            if y not in matched or dfs(matched[y]):
                matched[y] = x
                return True
    return False

N = int(sys.stdin.readline())
X = list(map(int, sys.stdin.readline().split()))
# X.sort()

# 소수 목록을 미리 준비
primes = []
for i in range(2, 2000):
    is_prime = True
    for j in range(2, i):
        if i % j == 0:
            is_prime = False
            break
    if is_prime: primes.append(i)
    else: continue

answers = []
for i in X:
    matched = {}
    if i == X[0]: continue
    if X[0] + i in primes:
        if N == 2:
            answers.append(i)
            break
        # print(i)
        # 첫번째 숫자와 현재 매치된 숫자를 제외한 새 리스트 생성
        Y = [x for x in X]
        del Y[0]
        del Y[Y.index(i)]
        matched = {}
        for y in Y:
            visited = [False for _ in range(len(Y))]
            dfs(y)
    
    # if matched: print(matched)
    if N != 2 and len(matched) == N - 2: answers.append(i)

if not answers:
    answers.append(-1)

answers.sort()

print(' '.join(list(map(str, answers))))

# 출처 : https://nerogarret.tistory.com/34

 

반응형
반응형

[백준] 2016번 제곱ㄴㄴ수 - PYTHON

import math

min, max = map(int, input().split())
 
NN = [1] * (max - min + 1) 

tmp_01 = []

for i in range(2, int(math.sqrt(max)) + 1):
    tmp_01.append(i ** 2)


for i in tmp_01:
    j = math.ceil(min / i)
    while i * j <= max:
        NN[i * j - min] = 0
        j += 1

print(sum(NN))
반응형
반응형

 

import sys
import math

A_size = int(sys.stdin.readline())
A = sys.stdin.readline().replace("\n", "").split(' ')
A = [int(i) for i in A]

# A를 오름차순으로 정렬하여 작은 숫자부터 순서대로 정리된 새로운 list를 할당 
sorted_A = [i for i in A]
sorted_A.sort()

P = []
# A의 각 숫자들에 대해 sorted_A에서의 index를 찾아 몇번째로 작은 숫자인지 P 수열에 새롭게 append함.
for i in A:
    P.append(sorted_A.index(i))
    # 이미 할당한 숫자는 sorted_A에서 -1로 대채해 재탐색되지 않도록 함.
    sorted_A[sorted_A.index(i)] = -1

results = [i for i in P]

for result in results:
    sys.stdout.write(str(result)+' ')
    
#  출처 : https://nerogarret.tistory.com/31
반응형
반응형

[PYTHON] 모듈 예제, 모듈 불러오기- 파이썬

 

mod1.py를 생성하고 다른 파일에서 모듈을 호출한다.

https://github.com/ngio/python_study/blob/main/module_py.py

 

GitHub - ngio/python_study: python, konply, numpy, matplotlib, networkx, pandas

python, konply, numpy, matplotlib, networkx, pandas - GitHub - ngio/python_study: python, konply, numpy, matplotlib, networkx, pandas

github.com

import mod1
print(" module name is ", mod1.__name__)

 

# mod1.py 
def add(a, b): 
    return a+b

def sub(a, b): 
    return a-b

# if __name__ == "__main__"을 사용하면 C:\doit>python mod1.py처럼 직접 이 파일을 실행했을 때는 __name__ == "__main__"이 참이 되어 
# if문 다음 문장이 수행된다. 
# 반대로 대화형 인터프리터나 다른 파일에서 이 모듈을 불러서 사용할 때는 __name__ == "__main__"이 거짓이 되어 
# if문 다음 문장이 수행되지 않는다.

if __name__ == "__main__":
    print(add(1, 4))
    print(sub(4, 2))
반응형

+ Recent posts