웅재의 코딩세상
[프로그래머스] 최대공약수와 최소공배수 본문
- 풀이
#include <string>
#include <vector>
using namespace std;
int gcd(int a, int b);
vector<int> solution(int n, int m) {
vector<int> answer;
int tmp=0, g=0, lcm=0;
if(n>m){
tmp = n;
n = m;
m = tmp;
}
g = gcd(n,m);
lcm = m*n / g;
answer.push_back(g);
answer.push_back(lcm);
return answer;
}
int gcd(int a, int b){
int c;
while(b !=0){
c = a%b;
a=b;
b=c;
}
return a;
}
반응형
'코딩테스트 > 프로그래머스 - LV 1' 카테고리의 다른 글
[프로그래머스] 제일 작은 수 제거하기 (0) | 2024.02.09 |
---|---|
[프로그래머스] 짝수와 홀수 (0) | 2024.02.09 |
[프로그래머스] 콜라츠 추측 (0) | 2024.02.09 |
[프로그래머스] 평균 구하기 (0) | 2024.02.09 |
[프로그래머스] 하샤드 수 (0) | 2024.02.09 |