hye-log

[프로그래머스]멀쩡한 사각형(Python) 본문

CodingTest/Programmers

[프로그래머스]멀쩡한 사각형(Python)

iihye_ 2022. 8. 19. 15:53

0. 문제 링크

https://school.programmers.co.kr/learn/courses/30/lessons/62048

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

1. 문제 설명

1) 가로, 세로의 최대공약수 구하기

2) 전체 사각형의 개수에서 부러진 사각형의 개수 빼기

 

2. 입출력

# input
w = 8
h = 12

# output
result = 80

 

3. 코드

def gcd(a, b):                                  # 최대공약수 함수
    if a % b == 0:                              # a를 b로 나눌 수 없으면
        return b                                # b return
    else:
        return gcd(b, a % b)                    # b와 a % b 의 최대공약수 찾기

def solution(w, h):
    answer = 1

    whole_square = w * h                        # 전체 사각형의 개수
    broken_square = w + h - gcd(w, h)           # 부러진 사각형의 개수
    answer = whole_square - broken_square

    return answer

 

실행 결과

 

4. Github

https://github.com/iihye/Algorithm/blob/main/Programmers/plane_square.py

 

GitHub - iihye/Algorithm

Contribute to iihye/Algorithm development by creating an account on GitHub.

github.com

 

728x90
Comments