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

2022. 8. 19. 15:53·CodingTest/Programmers

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
저작자표시 비영리 변경금지 (새창열림)

'CodingTest > Programmers' 카테고리의 다른 글

[프로그래머스]124 나라의 숫자(Python)  (0) 2022.08.26
[프로그래머스]H-Index(Python)  (0) 2022.08.23
[프로그래머스]오픈채팅방(Python)  (0) 2022.08.16
[프로그래머스]문자열 압축(Python)  (0) 2022.08.12
[프로그래머스]K번째수(Python)  (0) 2022.08.11
'CodingTest/Programmers' 카테고리의 다른 글
  • [프로그래머스]124 나라의 숫자(Python)
  • [프로그래머스]H-Index(Python)
  • [프로그래머스]오픈채팅방(Python)
  • [프로그래머스]문자열 압축(Python)
iihye_
iihye_
  • iihye_
    hye-log
    iihye_
    • 분류 전체보기 (293)
      • Development (17)
        • Spring (3)
        • Python (5)
        • JavaScript (4)
        • React (4)
        • Next.js (1)
      • Infra (15)
        • Docker (1)
        • Jenkins (2)
        • Nginx (3)
        • JBoss (1)
        • Windows (3)
        • Linux (3)
        • Kafka (2)
      • Database (1)
        • MongoDB (1)
      • Tools (1)
        • Git (0)
        • Github (1)
      • CodingTest (125)
        • Programmers (38)
        • Baekjoon (52)
        • SWEA (27)
        • Jungol (4)
        • Codetree (1)
        • Goorm (3)
      • Education (99)
        • SSAFY 10기 (5)
        • AI Tech 4기 (94)
      • NomadCoder (35)
        • (JS)크롬 앱 만들기 (35)
      • Notice (0)
  • 링크

    • Github
  • 인기 글

  • 최근 글

  • 전체
    오늘
    어제
  • hELLO· Designed By정상우.v4.10.5
iihye_
[프로그래머스]멀쩡한 사각형(Python)
상단으로

티스토리툴바