๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿš“ Self Study/๐Ÿ”“ Programmers

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค (๋ฉ€์ฉกํ•œ ์‚ฌ๊ฐํ˜•, Summer/Winter Coding(2019)) C++

by UKHYUN22 2022. 1. 3.
728x90

 

์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ "์œ ํด๋ฆฌ๋“œ ํ˜ธ์ œ๋ฒ•"์ด ๊ตฌ๊ธ€์— ๊ฐ€์žฅ ๋งŽ์ด ๋‚˜์™€์žˆ์–ด์„œ ํ•ด๋‹น ๋ฐฉ๋ฒ•์„ ์ตํ˜€๋ณด์ž ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉ์„ ํ–ˆ๋‹ค. ๊ฐ€๋กœ์™€ ์„ธ๋กœ๋ฅผ ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜๋กœ ๋‚˜๋ˆ„์–ด์„œ ๊ทœ์น™์„ ์ฐพ์•„๋ณด๋ ค๊ณ  ํ–ˆ์ง€๋งŒ ์ƒ๊ฐ๋ณด๋‹ค ๊ทœ์น™์„ ์ฐพ๋Š”๊ฒŒ ์‰ฝ์ง€ ์•Š์•„์„œ ํž˜๋“ค์—ˆ๋‹ค. GCD ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ• ํ•˜๋‚˜ ์ •๋„๋Š” ์™ธ์šฐ๋Š” ๊ฒƒ๋„ ๋‚˜์˜์ง€ ์•Š์•„๋ณด์ธ๋‹ค.

 

using namespace std;

long long gcd(int x, int y) {
    long long temp =0 ;
    while(y != 0) {
        temp = x % y;
        x = y;
        y = temp;
    }
    
    return x;
}

long long solution(int w,int h) {
    long long answer = 1;
    long long W = w;
    long long H = h;
    
    long long wrong = W+H - gcd(w,h);
    return (W*H)-wrong;
}