본문으로 바로가기

HackerRank Python Solution / Division

category Develop/hackerrank.com 2021. 11. 12. 19:13

 

Task
The provided code stub reads two integers,  a and b, from STDIN.

Add logic to print two lines. The first line should contain the result of integer division,  a//b . The second line should contain the result of float division, a/b .

No rounding or formatting is necessary.

Example

 

Division

if __name__ == '__main__':
    a = int(input())
    b = int(input())
    
    print(a//b)
    print(a/b)

 

 

해설

숫자형연산자 기능을 통해 값과 몫을 반환한다.

 

숫자형 연산자

# 나눗셈 후 값 반환
>>> 7/3
2.3333333333333335

# 나눗셈 후 몫 반환
>>> 7//3
2

# 나눗셈 후 나머지 반환
>>> 7 % 3
1

# 곱셈
>>> 7 * 3
21

# 거듭제곱 
>>> 7 ** 3  # 7의 3제곱 
343

 

divmod

>>> divmod(7, 3)
(2, 1)

>>> 몫, 나머지 = divmod(7, 3)
>>> 몫
2
>>> 나머지
1

파이썬 내장함수 divmod는 숫자형연산자 기능과 유사하다