import sys
n=int(sys.stdin.readline())
for i in range(1,n+1):
    print(i)

 

sys.stdin.readline()을 사용하기 위해서 가장 먼저 sys를 import하는 것을 잊지 않도록 하자.

 

 

 

문제 : https://www.acmicpc.net/problem/2741

 

2741번: N 찍기

자연수 N이 주어졌을 때, 1부터 N까지 한 줄에 하나씩 출력하는 프로그램을 작성하시오.

www.acmicpc.net

 

'Baekjoon > python' 카테고리의 다른 글

[백준 11021번] A+B - 7  (0) 2022.04.21
[백준 2742번] 기찍 N  (0) 2022.04.21
[백준 15552번] 빠른 A+B  (0) 2022.04.20
[백준 8393번] 합  (0) 2022.04.20
[백준 10950번] A+B - 3  (0) 2022.04.20
import sys
T=int(input())
for i in range(1,T+1):
    num1,num2=map(int,sys.stdin.readline().split())
    print(num1+num2)

 

for문에서 입출력 방식이 느리면 한 두줄이 아닌 여러줄을 입력받거나 출력할 때 시간 초과가 날 수 있다.

그러므로 input 대신 sys.stdin.readline을 사용해야 한다.

sys.stdin.readline을 사용하기 위해서는 먼저 sys를 import 해주어야한다는 점을 잊지말자.

 

 

 

문제: https://www.acmicpc.net/problem/15552

 

15552번: 빠른 A+B

첫 줄에 테스트케이스의 개수 T가 주어진다. T는 최대 1,000,000이다. 다음 T줄에는 각각 두 정수 A와 B가 주어진다. A와 B는 1 이상, 1,000 이하이다.

www.acmicpc.net

'Baekjoon > python' 카테고리의 다른 글

[백준 2742번] 기찍 N  (0) 2022.04.21
[백준 2741번] N 찍기  (0) 2022.04.21
[백준 8393번] 합  (0) 2022.04.20
[백준 10950번] A+B - 3  (0) 2022.04.20
[백준 2739번] 구구단  (0) 2022.04.19
n=int(input())
sum=0
for i in range(1,n+1):
    sum+=i
print(sum)

 

 

 

 

문제 : https://www.acmicpc.net/problem/8393

 

8393번: 합

n이 주어졌을 때, 1부터 n까지 합을 구하는 프로그램을 작성하시오.

www.acmicpc.net

 

'Baekjoon > python' 카테고리의 다른 글

[백준 2741번] N 찍기  (0) 2022.04.21
[백준 15552번] 빠른 A+B  (0) 2022.04.20
[백준 10950번] A+B - 3  (0) 2022.04.20
[백준 2739번] 구구단  (0) 2022.04.19
[백준 2480번] 주사위 세 개  (0) 2022.04.16
T=int(input())
i=1
while i<=T:
    num1,num2=map(int,input().split())
    print(num1+num2)
    i+=1

 

 

 

 

문제 : https://www.acmicpc.net/problem/10950

 

10950번: A+B - 3

두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오.

www.acmicpc.net

 

'Baekjoon > python' 카테고리의 다른 글

[백준 15552번] 빠른 A+B  (0) 2022.04.20
[백준 8393번] 합  (0) 2022.04.20
[백준 2739번] 구구단  (0) 2022.04.19
[백준 2480번] 주사위 세 개  (0) 2022.04.16
[백준 2525번] 오븐 시계  (0) 2022.04.15
num=int(input())
for i in range(1,10):
    print(num,'*',i,'=',num*i)

 

 

 

 

문제 : https://www.acmicpc.net/problem/2739

 

2739번: 구구단

N을 입력받은 뒤, 구구단 N단을 출력하는 프로그램을 작성하시오. 출력 형식에 맞춰서 출력하면 된다.

www.acmicpc.net

 

'Baekjoon > python' 카테고리의 다른 글

[백준 8393번] 합  (0) 2022.04.20
[백준 10950번] A+B - 3  (0) 2022.04.20
[백준 2480번] 주사위 세 개  (0) 2022.04.16
[백준 2525번] 오븐 시계  (0) 2022.04.15
[백준 2884번] 알람 시계  (0) 2022.04.14
a,b,c=map(int,input().split())
list=[]
list.append(a)
list.append(b)
list.append(c)
list.sort()
if a==b and b==c:
    print(10000+a*1000)
elif a==b and b!=c:
    print(1000+a*100)
elif a==c and a!=b:
    print(1000+a*100)
elif b==c and b!=a:
    print(1000+b*100)
else:
    print(int(list[2])*100)

 

처음에는 이렇게 풀었다.

푼 코드를 다시 보며 생각해보니 문제에서의 키워드는 "중복"인 것 같았다. 중복과 관련된 함수는 set함수이므로 set함수를 이용해 다시 작성해 보았다.

list = list(map(int, input().split()))

if len(set(list))==1: 
    print(list[0]*1000+10000)
elif len(set(list))==2: 
    print(sorted(list)[1]*100+1000) #정렬 후 두번 째 값은 항상 중복된 값
else: 
    print(max(list)*100)

 

다시보면 처음 코드는 너무 의식의 흐름대로 작성해 길어진 것 같다.

생각해보니 list에 int 형태로 원소를 넣어줬기 때문에 처음 풀이 마지막 줄에서 굳이 int로 변환해주지 않아도 되겠다.

 

 

 

문제 : https://www.acmicpc.net/problem/2480

 

2480번: 주사위 세개

1에서부터 6까지의 눈을 가진 3개의 주사위를 던져서 다음과 같은 규칙에 따라 상금을 받는 게임이 있다.  같은 눈이 3개가 나오면 10,000원+(같은 눈)×1,000원의 상금을 받게 된다.  같은 눈이 2개

www.acmicpc.net

 

'Baekjoon > python' 카테고리의 다른 글

[백준 10950번] A+B - 3  (0) 2022.04.20
[백준 2739번] 구구단  (0) 2022.04.19
[백준 2525번] 오븐 시계  (0) 2022.04.15
[백준 2884번] 알람 시계  (0) 2022.04.14
[백준 14681번] 사분면 고르기  (0) 2022.04.13
h,m=map(int,input().split())
plus_m=int(input())
if m+plus_m<60:
    print(h,m+plus_m)
else:
    i=(m+plus_m)//60
    if h+i<24:
        print(h+i,m+plus_m-60*i)
    else:
        print(h+i-24,m+plus_m-60*i)

 

계속 틀렸다고 나와서 수정을 거쳤는데 반례찾기가 힘들었다.

직접 프로그램을 계속 돌리며 반례 찾기에 번거로울 때는 손으로 써보기!

 

마치 미적분에서 미분불능 점을 경계값에서 주로 찾았듯이 반례도 가장 먼저 경계값을 집중적으로 고려하면 좋은 것 같다.

 

 

 

문제 : https://www.acmicpc.net/problem/2525

 

2525번: 오븐 시계

첫째 줄에 종료되는 시각의 시와 분을 공백을 사이에 두고 출력한다. (단, 시는 0부터 23까지의 정수, 분은 0부터 59까지의 정수이다. 디지털 시계는 23시 59분에서 1분이 지나면 0시 0분이 된다.)

www.acmicpc.net

'Baekjoon > python' 카테고리의 다른 글

[백준 2739번] 구구단  (0) 2022.04.19
[백준 2480번] 주사위 세 개  (0) 2022.04.16
[백준 2884번] 알람 시계  (0) 2022.04.14
[백준 14681번] 사분면 고르기  (0) 2022.04.13
[백준 2753번] 윤년  (0) 2022.04.13
h,m=map(int,input().split())
if h>=1 and m>=45:
    print(h,m-45)
elif h>=1 and m<45:
    n=int(60-abs(m-45))
    print(h-1,n)
elif h==0 and m>=45:
    print(h,m-45)
elif h==0 and m<45:
    n=int(60-abs(m-45))
    print(23,n)

 

 

 

 

문제 : https://www.acmicpc.net/problem/2884

 

2884번: 알람 시계

상근이는 매일 아침 알람을 듣고 일어난다. 알람을 듣고 바로 일어나면 다행이겠지만, 항상 조금만 더 자려는 마음 때문에 매일 학교를 지각하고 있다. 상근이는 모든 방법을 동원해보았지만,

www.acmicpc.net

 

'Baekjoon > python' 카테고리의 다른 글

[백준 2480번] 주사위 세 개  (0) 2022.04.16
[백준 2525번] 오븐 시계  (0) 2022.04.15
[백준 14681번] 사분면 고르기  (0) 2022.04.13
[백준 2753번] 윤년  (0) 2022.04.13
[백준 9498번] 시험성적  (0) 2022.04.07
x=int(input())
y=int(input())
if x>0:
    if y>0:
        print(1)
    else:
        print(4)
else:
    if y>0:
        print(2)
    else:
        print(3)

 

 

 

 

문제 : https://www.acmicpc.net/problem/14681

'Baekjoon > python' 카테고리의 다른 글

[백준 2525번] 오븐 시계  (0) 2022.04.15
[백준 2884번] 알람 시계  (0) 2022.04.14
[백준 2753번] 윤년  (0) 2022.04.13
[백준 9498번] 시험성적  (0) 2022.04.07
[백준 1330번] 두 수 비교하기  (0) 2022.04.07
a=int(input())
if a%4==0 and (a%100!=0 or a%400==0):
    print(1)
else:
    print(0)

 

 

 

 

문제 : https://www.acmicpc.net/problem/2753

'Baekjoon > python' 카테고리의 다른 글

[백준 2884번] 알람 시계  (0) 2022.04.14
[백준 14681번] 사분면 고르기  (0) 2022.04.13
[백준 9498번] 시험성적  (0) 2022.04.07
[백준 1330번] 두 수 비교하기  (0) 2022.04.07
[백준 2588번] 곱셈  (0) 2022.04.07
a=int(input())
if a>=90 and a<=100:
    print("A")
elif a>=80 and a<=89:
    print("B")
elif a>=70 and a<=79:
    print("C")
elif a>=60 and a<=69:
    print("D")
else:
    print("F")

 

항상 부등호 다음에 등호가 와야한다. =< 과 같이 하면 오류가 난다.

 

 

 

문제 : https://www.acmicpc.net/problem/9498

 

'Baekjoon > python' 카테고리의 다른 글

[백준 14681번] 사분면 고르기  (0) 2022.04.13
[백준 2753번] 윤년  (0) 2022.04.13
[백준 1330번] 두 수 비교하기  (0) 2022.04.07
[백준 2588번] 곱셈  (0) 2022.04.07
[백준 10430번] 나머지  (0) 2022.04.07
a,b=map(int,input().split())
if a>b:
    print(">")
elif a<b:
    print("<")
else:
    print("==")

 

 

 

 

문제 : https://www.acmicpc.net/problem/1330

'Baekjoon > python' 카테고리의 다른 글

[백준 2753번] 윤년  (0) 2022.04.13
[백준 9498번] 시험성적  (0) 2022.04.07
[백준 2588번] 곱셈  (0) 2022.04.07
[백준 10430번] 나머지  (0) 2022.04.07
[백준 18108번] 1998년생인 내가 태국에서는 2541년생?!  (0) 2022.04.07

+ Recent posts