문제접근
•
isPrime인 경우에만 stack에 저장
•
stack에서 하나씩 꺼내보면서 lcm을 구함
•
lcm = a * b / gcd(a,b)
놓쳤던 부분
•
answer의 자료형만 신경 썼는데 결국 lcm의 계산과정에서 gcd의 반환값, 매개변수 모두 쓰이기 때문에 자료형 고려를 해줘야함
•
lcm 공식을 잘못 알고 있었음
코드
2168 KB
36 ms
#include <iostream>
#include <algorithm>
#include <cmath>
#include <stack>
using namespace std;
bool isPrime(int a)
{
int max_idx = sqrt(a);
bool isDivided = false;
for (int i = 2; i <= max_idx; i++)
{
if (a % i == 0)
{
isDivided = true;
break ;
}
}
if (isDivided)
return (false);
return (true);
}
long long gcd(long long a, long long b)
{
if (b == 0)
return (a);
return (gcd(b, a % b));
}
long long lcm(long long a, long long b)
{
return (a * b / gcd(a, b));
}
int main(void)
{
int n;
int input;
stack<long long> s;
long long answer;
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> input;
if (isPrime(input))
s.push(input);
}
if (s.empty())
cout << "-1";
else
{
answer = s.top();
s.pop();
while (!s.empty())
{
answer = lcm(max(answer, s.top()), min(answer, s.top()));
s.pop();
}
cout << answer;
}
return (0);
}
C++
복사