Notice
Recent Posts
문제 풀이 및 개발 공간
[백준] 21919번 소수 최소 공배수 문제! (silver 3 본문
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
int n=Integer.parseInt(br.readLine());
int max=0;
int[] num=new int[n];
StringTokenizer st=new StringTokenizer(br.readLine());
for(int i=0; i<n; i++) {
num[i]=Integer.parseInt(st.nextToken());
max= max>num[i] ? max: num[i];
}
int[] aratos=new int[max+1];
aratos=aratosChae(aratos, max);
long multy=1;
for(int i=0; i<n; i++) {
if(aratos[num[i]] ==0) {
multy*=num[i];
aratos[num[i]]=1; //중복인 상황들 잡아주기(중복되지 않게)
}
}
bw.write(multy==1 ? "-1": multy+"" );
bw.flush();
}
public static int[] aratosChae(int[] aratos, int max) {
for(int i=2; i<max+1; i++) {
if(aratos[i]==1) {
continue;
}
for(int j=i+i; j<max+1; j+=i) {
aratos[j]=1;
}
}
return aratos;
}
}
'백준공부 > java' 카테고리의 다른 글
[백준] 1951번 활자 문제! (silver 3 (0) | 2022.08.18 |
---|---|
[백준] 1302번 베스트셀러 문제! (silver 4 (0) | 2022.08.18 |
[백준] 5800번 성적 통계 문제! (silver 5 (0) | 2022.08.18 |
[백준] 9076번 점수 집계 문제! (bronze 2 (0) | 2022.08.18 |
[백준] 2693번 N번째 큰 수 문제! (bronze 1 (0) | 2022.08.17 |