프로그래머스 LV2 - N개의 최소공배수
2022. 11. 10. 11:07ㆍ코딩테스트/프로그래머스
문제
https://school.programmers.co.kr/learn/courses/30/lessons/12953
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
문제풀이
참고했던 자료
https://twpower.github.io/69-how-to-get-gcd-and-lcm
위에 공식을 사용하여 순차적으로 최소 공약수를 구하며 진행해 나갔다.
코드
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
|
#include <string>
#include <vector>
using namespace std;
int gcd(int a , int b)
{
while(b!=0){
int r = a%b;
a= b;
b= r;
}
return a;
}
int solution(vector<int> arr) {
int answer = 0;
answer = arr[0];
for(int i = 1 ; i < arr.size();i++)
{
int mul = answer * arr[i];
answer = answer>arr[i]?gcd(answer,arr[i]):gcd(arr[i],answer);
answer = mul/answer;
}
return answer;
}
|
cs |
'코딩테스트 > 프로그래머스' 카테고리의 다른 글
연습 LV2 - 예상대진표 (0) | 2022.11.11 |
---|---|
연습LV2 - 점프와 순간이동 (0) | 2022.11.11 |
프로그래머스 LV2 - 구명보트 (0) | 2022.11.10 |
프로그래머스 LV2 - 영어끝말잇기 (0) | 2022.11.09 |
프로그래머스 Lv2 - 짝지어 제거하기 (0) | 2022.11.09 |