아이디어 : 나누어 떨어지는 수가 2인 경우 소수인 것을 활용한다
ArrayList를 활용하여 동적 배열을 활용한다.
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
ArrayList<Integer> list = new ArrayList<>();
int M = sc.nextInt();
int N = sc.nextInt();
int count = 0;
int min = 100000;
int sum = 0;
for (int i = M; i <= N; i++) {
int countX = 0;
for (int j = 1; j <= i; j++) {
if (i % j == 0) {
countX++;
}
}
if (countX == 2) {
count++;
list.add(i);
}
}
for (int i = 0; i < list.size(); i++) {
if (min >= list.get(i)) {
min = list.get(i);
}
sum += list.get(i);
}
if (list.size() > 0) {
System.out.println(sum);
System.out.print(min);
} else if (list.size() == 0) {
System.out.print(-1);
}
sc.close();
}
}'java' 카테고리의 다른 글
| 백준 1085 직사각형 탈출 자바 java (0) | 2024.01.12 |
|---|---|
| 백준 11653 소인수분해 자바 java (0) | 2024.01.12 |
| 백준 2869 달팽이 자바 java (0) | 2024.01.11 |
| 백준 2292 벌집 자바 java (0) | 2024.01.11 |
| 백준 11005 진법변환2 10진수를 N진법으로 java 자바 (0) | 2024.01.11 |