https://programmers.co.kr/learn/courses/30/lessons/12940
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
#include <vector>
using namespace std;
//최대공약수
int gcd(int a, int b) {
int gcd = 0;
int less_than = a < b ? a : b;
for (int i = 1; i < less_than + 1; i++) {
if (a%i == 0 && b%i == 0) {
gcd = i;
}
}
return gcd;
}
//최소공배수
int lcm(int a, int b) {
int g = gcd(a, b);
if (g == 1) {
return a * b;
}
return a*b/g;
}
vector<int> solution(int n, int m) {
vector<int> answer;
answer.push_back(gcd(n, m));
answer.push_back(lcm(n, m));
return answer;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
반응형
'알고리즘(C++) > 프로그래머스 level1' 카테고리의 다른 글
핸드폰 번호 가리기 (0) | 2020.04.03 |
---|---|
평균 구하기 (0) | 2020.04.03 |
제일 작은 수 제거하기 (0) | 2020.04.03 |
정수 내림차순으로 배치하기 (0) | 2020.04.03 |
자릿수 더하기 (0) | 2020.04.03 |