https://programmers.co.kr/learn/courses/30/lessons/12953
코딩테스트 연습 - N개의 최소공배수
두 수의 최소공배수(Least Common Multiple)란 입력된 두 수의 배수 중 공통이 되는 가장 작은 숫자를 의미합니다. 예를 들어 2와 7의 최소공배수는 14가 됩니다. 정의를 확장해서, n개의 수의 최소공배
programmers.co.kr
나의 코드
#include <string>
#include <vector>
using namespace std;
int uclid(int a, int b) { // 최대공약수
if (a % b == 0) return b;
else return uclid(b, a % b);
}
int solution(vector<int> arr) { //a*b = 최소공배수*최대공약수
for (int i = 0; i < arr.size() - 1; i++) {
int gcd = uclid(arr[i], arr[i + 1]);
arr[i + 1] = (arr[i] * arr[i + 1]) / gcd;
}
return arr[arr.size() - 1];
}'전공공부 > 코딩테스트' 카테고리의 다른 글
| (c++) 프로그래머스 "양궁대회" (0) | 2022.04.24 |
|---|---|
| (c++) 백준 "1244 : 스위치 켜고 끄기" (0) | 2022.04.22 |
| (c++) 프로그래머스 "행렬의 곱셈" (0) | 2022.04.21 |
| (c++) 프로그래머스 "파보나치 수" (0) | 2022.04.20 |
| (c++) 프로그래머스 "최솟값 만들기" (0) | 2022.04.19 |