일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- 라이징캠프
- Segue
- prefix()
- MVC
- joined()
- suffix()
- 알고리즘
- 싱글톤
- 대문자소문자
- 프로그래머스
- swift
- Autolayout
- reduce()
- replacesubrange()
- 클론코딩
- API
- uikit
- Info.plist
- 코딩테스트
- tableView
- String()
- IOS
- zip()
- github
- alamofire
- components()
- uisearchbar
- GIT
- xib
- 코드업 파이썬 기초 100제
- Today
- Total
Daeng iOS
[코드업 CodeUp] Python 기초 100제 (6041~6050) 본문
[코드업 CodeUp] Python 기초 100제 (6041~6050)
U_Daeng 2022. 3. 5. 20:40[문제집 링크]
https://codeup.kr/problemsetsol.php?psid=33
문제집 / Python 기초 100제
codeup.kr
[깃허브]
https://github.com/yujeong-kwon/Coding-test/tree/master/Codeup
GitHub - yujeong-kwon/Coding-test
Contribute to yujeong-kwon/Coding-test development by creating an account on GitHub.
github.com
6041부터 6050까지는 산술연산, 값변환, 비트시프트연산, 비교연산이 섞여있었다.
#6041 - 정수 2개 입력받아 나눈 나머지 계산하기
정수 2개(a, b)를 입력받아 a를 b로 나눈 나머지를 출력
내 풀이
import sys
input = sys.stdin.readline
a, b = map(int,input().rstrip().split())
print(a%b)
모범 풀이
a, b = input().split()
print(int(a)%int(b))
나머지를 구할 때는 모듈러 연산(%)를 사용한다.
#6042 - 실수 1개 입력받아 소숫점이하 자리 변환하기
실수 1개를 입력받아 소숫점 이하 두 번째 자리까지의 정확도로 반올림한 값 출력
내 풀이
import sys
input = sys.stdin.readline
a = float(input().rstrip())
print(format(a,".2f"))
모범 풀이
f = float(input())
print(round(f,2))
두 번째 자리 까지 반올림 하기 위해서는 format(변수, "지정할 형식") 으로 .2f를 지정하거나
반올림해주는 함수 round(변수, 자릿수) 를 사용한다.
#6043 - 실수 2개 입력받아 나눈 결과 계산하기
실수 2개(f1, f2)를 입력받아 f1을 f2로 나눈 값을 출력
내 풀이
import sys
input = sys.stdin.readline
a, b = map(float,input().rstrip().split())
print(format(a/b, ".3f"))
모범 풀이
a,b=input().split()
a=float(a)
b=float(b)
c=a/b
print('%.3f'%c)
a,b=input().split()
a=float(a)
b=float(b)
c=a/b
print(format(c,".3f"))
형식을 지정해서 출력하는 방법으로 print('형식'%변수) 도 있다.
#6044 - 정수 2개 입력받아 자동 계산하기
정수 2개(a, b)를 입력받아 합, 차, 곱, 몫, 나머지, 나눈 값을 자동으로 계산
내 풀이
import sys
input = sys.stdin.readline
a, b = map(int, input().rstrip().split())
print("%d\n%d\n%d\n%d\n%d"%(a+b,a-b,a*b,a/b,a%b))
print(format(float(a)/float(b), ".2f"))
import sys
input = sys.stdin.readline
a, b = map(int, input().rstrip().split())
#print("%d\n%d\n%d\n%d\n%d"%(a+b,a-b,a*b,a/b,a%b))
#print(format(float(a)/float(b), ".2f"))
print(a+b,a-b,a*b,a//b,a%b,round(a/b,2),sep="\n")
모범 풀이
a,b=input().split()
a=int(a)
b=int(b)
print(a+b)
print(a-b)
print(a*b)
print(a//b)
print(a%b)
print(round(a/b,2))
#6045 - 정수 3개 입력받아 합과 평균 출력하기
정수 3개를 입력받아 합과 평균을 출력
내 풀이
import sys
input = sys.stdin.readline
a, b, c = map(int, input().rstrip().split())
print("%d %0.2f"%(a+b+c, (a+b+c)/3))
모범 풀이
a, b, c = input().split()
a=int(a)
b=int(b)
c=int(c)
hap=a+b+c
avg=hap/3
print(hap, format(avg, ".2f"))
#6046 - 정수 1개 입력받아 2배 곱해 출력하기
정수 1개를 입력받아 2배 곱해 출력
내 풀이
import sys
input = sys.stdin.readline
a = int(input().rstrip())
print(a*2)
모범 풀이
n = int(input())
print(n<<1)
나는 그대로 2를 곱했는데 시프트(<<)연산을 사용하는 문제였다!
<< 는 비트가 왼쪽으로 한칸씩 이동하기 때문에 값이 2배가 된다.
나누기를 하고싶다면 >>
#6047 - 2의 거듭제곱 배로 곱해 출력하기
정수 2개(a, b)를 입력받아 a를 2ᵇ배 곱한 값으로 출력
내 풀이
import sys
input = sys.stdin.readline
a, b = map(int,input().rstrip().split())
print(a << b)
모범 풀이
a, b = input().split()
a = int(a)
b = int(b)
print(a<<b)
#6048 - 정수 2개 입력받아 비교하기1
두 정수(a, b)를 입력받아 a가 b보다 작으면 True를, a가 b보다 크거나 같으면 False 를 출력
내 풀이
import sys
input = sys.stdin.readline
a, b = map(int,input().rstrip().split())
if a < b:
print("True")
else:
print("False")
모범 풀이
a, b = input().split()
a = int(a)
b = int(b)
print(a<b)
굳이 조건문을 사용하지 않아도 그냥 비교 연산을 출력문에 넣으면 True / False를 출력한다.
#6049 - 정수 2개 입력받아 비교하기2
두 정수(a, b)를 입력받아 a가 b보다 같으면 True를, 같지 않으면 False 를 출력
내 풀이
import sys
input = sys.stdin.readline
a, b = map(int, input().rstrip().split())
if a == b:
print("True")
else:
print("False")
모범 풀이
a, b = input().split()
a = int(a)
b = int(b)
print(a==b)
프로그래밍에서 같은지 다른지를 비교할 땐 == 사용! 그냥 = 는 대입 연산자
#6050 - 정수 2개 입력받아 비교하기3
두 정수(a, b)를 입력받아 b의 값이 a의 값 보다 크거나 같으면 True 를, 같지 않으면 False 를 출력
내 풀이
import sys
input = sys.stdin.readline
a, b = map(int, input().rstrip().split())
if a <= b:
print("True")
else:
print("False")
모범 풀이
a, b = input().split()
a = int(a)
b = int(b)
print(a<=b)
'알고리즘 & 코딩테스트 > CodeUp : Python 기초 100제' 카테고리의 다른 글
[코드업 CodeUp] Python 기초 100제 (6061~6070) (0) | 2022.08.26 |
---|---|
[코드업 CodeUp] Python 기초 100제 (6051~6060) (0) | 2022.08.26 |
[코드업 CodeUp] Python 기초 100제 (6031~6040) (0) | 2022.02.08 |
[코드업 CodeUp] Python 기초 100제 (6021~6030) (0) | 2022.02.07 |
[코드업 CodeUp] Python 기초 100제 (6011~6020) (0) | 2022.02.05 |