๋ฐ์ํ
์ต๋๊ณต์ฝ์, ์ต์๊ณต๋ฐฐ์ ๊ตฌํ๊ธฐ ์๊ณ ๋ฆฌ์ฆ
1. ๋ฐ๋ณต๋ฌธ ๋๋ฆฌ๊ธฐ
int a = 12, b = 3;
int gcd = 0 , lcm = 0;
int r = 1;
//์ต๋๊ณต์ฝ์ ๊ตฌํ๋ ๋ฉ์๋
int gcd( int a , int b ){
while( r > 0 ){
r = a % b;
a = b;
b = r;
}
return a;
}
gcd = gcd(a , b);
lcm = (a * b) / gcd;
2. ์ฌ๊ท ํจ์ ์ฌ์ฉ
int a = 12, b = 3;
int gcd = 0, lcm = 0;
//๋ฐ๋ณต๋ฌธ์ฒ๋ผ ๋๋ ์ฌ๊ทํจ์.
int makeGCD( int a, int b){
if( b == 0){
return a;
} else {
return gcd( b, a%b );
}
}
gcd = makeGCD(a , b);
lcm = ( a * b ) / gcd;
๋ฐ์ํ