{"componentChunkName":"component---src-templates-blog-post-js","path":"/Algorithm/프로그래머스-멀쩡한사각형-Python/","result":{"data":{"site":{"siteMetadata":{"title":"hyesungoh","author":"hyesungoh","siteUrl":"https://www.hyesungoh.xyz","comment":{"disqusShortName":"","utterances":"hyesungoh/blog"},"sponsor":{"buyMeACoffeeId":""}}},"markdownRemark":{"id":"2dd5e64b-71a2-506a-9835-c1996010df9f","excerpt":"가로 길이가 W, 세로 길이가 H인 직사각형이 았다. 종이는 1, 1 크기의 격자 형태로 선이 그어져 있다. 대각선 꼭지점 2개를 잇는 방향으로 잘라 놓았을 때, 온전한 사각형의 수를 구하는 문제. 최대공약수를 이용해야하는 것은 알았지만 어떤 방식으로 이용해야되는 지 감을 잡기 어려웠다. 검색을 통해 (w * h) - (w + h - gcd(w, h…","html":"<p>가로 길이가 W, 세로 길이가 H인 직사각형이 았다. 종이는 1, 1 크기의 격자 형태로 선이 그어져 있다. 대각선 꼭지점 2개를 잇는 방향으로 잘라 놓았을 때, 온전한 사각형의 수를 구하는 문제. 최대공약수를 이용해야하는 것은 알았지만 어떤 방식으로 이용해야되는 지 감을 잡기 어려웠다. <a href=\"https://taesan94.tistory.com/55F\">검색</a>을 통해 (w * h) - (w + h - gcd(w, h))가 공식인 것을 알게 됐다. 첫 번째 풀이는 유클리드 호제 방법을 이용하여 최대공약수를 구하는 것을 작성하여 풀었으며 두 번째 풀이는 내장 라이브러리의 gcd를 이용하여 풀었다.</p>\n<div class=\"gatsby-highlight\" data-language=\"python\"><pre class=\"language-python\"><code class=\"language-python\"><span class=\"token keyword\">def</span> <span class=\"token function\">gcd</span><span class=\"token punctuation\">(</span>x<span class=\"token punctuation\">,</span> y<span class=\"token punctuation\">)</span><span class=\"token punctuation\">:</span>\n    <span class=\"token keyword\">while</span> y<span class=\"token punctuation\">:</span>\n        x<span class=\"token punctuation\">,</span> y <span class=\"token operator\">=</span> y<span class=\"token punctuation\">,</span> x <span class=\"token operator\">%</span> y\n    <span class=\"token keyword\">return</span> x\n\n\n<span class=\"token keyword\">def</span> <span class=\"token function\">solution</span><span class=\"token punctuation\">(</span>w<span class=\"token punctuation\">,</span> h<span class=\"token punctuation\">)</span><span class=\"token punctuation\">:</span>\n    n <span class=\"token operator\">=</span> gcd<span class=\"token punctuation\">(</span>w<span class=\"token punctuation\">,</span> h<span class=\"token punctuation\">)</span>\n    <span class=\"token keyword\">return</span> <span class=\"token punctuation\">(</span>w <span class=\"token operator\">*</span> h<span class=\"token punctuation\">)</span> <span class=\"token operator\">-</span> <span class=\"token punctuation\">(</span>w <span class=\"token operator\">+</span> h <span class=\"token operator\">-</span> n<span class=\"token punctuation\">)</span>\n\n<span class=\"token comment\"># from math import gcd</span>\n<span class=\"token comment\">#</span>\n<span class=\"token comment\"># def solution(w, h):</span>\n<span class=\"token comment\">#     return (w * h) - (w + h - gcd(w, h))</span></code></pre></div>","frontmatter":{"title":"프로그래머스-멀쩡한사각형 - Python","date":"February 21, 2021"}}},"pageContext":{"slug":"/Algorithm/프로그래머스-멀쩡한사각형-Python/","previous":{"fields":{"slug":"/Algorithm/BOJ-10867-Javascript/"},"frontmatter":{"title":"BOJ-10867 - Javascript","category":"Algorithm","draft":false}},"next":{"fields":{"slug":"/Algorithm/프로그래머스-가장큰수-Python/"},"frontmatter":{"title":"프로그래머스-가장큰수 - Python","category":"Algorithm","draft":false}}}},"staticQueryHashes":["3128451518","3290666882"]}