Code&Data Insights

BaekJoon Algorithm - Stage 3 [ 1-11 ] ( Python 3 ) 본문

Algorithm/BaekJoon Online Judge

BaekJoon Algorithm - Stage 3 [ 1-11 ] ( Python 3 )

paka_corn 2022. 1. 15. 00:13

2022.01.13

 

#1  Python - for문

for문 range(0,n+1)

--> 0부터 n까지 

자동으로 1씩 increment 

설정하려면 (0,n+1,3)

마지막에 쓰면, 3씩 increment

 

#2  A+B를 여러 번 출력하는 문제

[my code]

t = int(input())

for i in range(1,t+1):
    i = input().split(' ')
    a = int(i[0])
    b = int(i[1])
    print(int(i[0])+int(i[1]))

 

---> 입력 5번 한번에 가능하지 않고, 한번씩 하고 답이 나옴 

정답으로 인정되긴 했으나 찜찜..

---> range(1,t+1) 쓰지않고, 그냥 range(t)도 가능

 

=> 다른 사람들도 비슷한 방식으로 추출됨

 

* map function * 

 

 

 

 

#3  input() and sys.stdin.readline()

sys.stadin.readline을 쓰는 이유는 반복문에서 여러 문장을 처리할때 시간초과가 되지 않음.

import sis를 해줘야함! 

 

 

 

 

 

#4 Decrementing With range()

for i in reversed(range(n)):
    print(i)

*  reversed() also works with strings

 

 

 

 

 

 

#5 str() function

The str() function converts the specified value into a string.

숫자를 문자열과 같이 쓰기 위해 문자열로 변경, +로 사이를 연결 

or String formatting을 해 표현도 가능 

(ex. 백준 11021번, A+B-7)

 

 

 

 

 

 

 

 

 

#6 Print the pattern(string)

 

By default, the print function ends with a newline. Passing the whitespace to the end parameter (end=' ') 

 * used to add any string * 

같은 라인에 출력할 수 있게 하기위해 사용 (for nested loops)

 

 

 

 

 

 

 

 

 

#7 Print the pattern(string)

java에서 쓰던대로 nestep for loop을 사용했는데, 

다른 분들이 푸신거보니 생각보다 더 간단했다. 

 

[my code]

n = int(input())

for i in range(1,n+1):
    for j in range(n-i):
        print(" ",end='')
    for k in range(1,i+1):
        print("*",end='')
    print('')

 

 

[new code]

n = int(input())
for i in range(1,n+1):
        print(" "*(n-i)+"*"*i)

 

 

 

 

 

 

 

#8 10871 - x보다 작은수

진짜 안풀렸던 문제..

 

[my code]

n,x = map(int,input().split())
a = list(map(int,input().split()))

for i in range(n):
    if a[i]< x:
        print(a[i],end='')

 

*** end=' '  vs end='' ***

-> 두개 다르다. 공백 있는거 vs 없는거

 

 

 

 

 

 

 

 

 

 

 

 

 

Comments