250x250
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 완탐
- 삼성역테
- 재귀함수
- 자바 코테
- 자바
- 알고리즘개념
- 주사위굴리기2
- 볼륨 만들기
- Bfs와DFS
- 완전탐색
- 중복조합
- BFS
- D드라이브생성
- 코테준비
- 백준13458
- 전화번호속의암호
- 백준
- 백준2251
- 백준15652
- 정올 1620
- 23288
- 정보처리기사
- 순열
- 코테
- java
- 알고리즘
- 에라토스테네스의채
- N과M
- 중복순열
- 파티션 크기 조정
Archives
- Today
- Total
뚱땅뚱땅
[문제] 백준 11653번 소인수분해 본문
728x90
* 출처: 백준 단계별로 풀어보기 기본수학 2
11653번: 소인수분해
첫째 줄에 정수 N (1 ≤ N ≤ 10,000,000)이 주어진다.
www.acmicpc.net
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(in.readLine());
int s = 2;
while(true) {
if(N == 1) return;
if(isSosu(N)) {
System.out.println(N);
break;
}
if(N%s == 0) {
System.out.println(s);
N = N/s;
}else {
s++;
}
}
}
static boolean isSosu(int n) {
if(n==1) return false;
if(n==2) return true;
for(int i=2;i<n;i++) {
if(n%i == 0) {
return false;
}
}
return true;
}
}
** 다른 사람 문제 풀이
어떤 N이 두 개 이상 곱셈으로 나타날 수 있을 때 인수 중 한개는 반드시 N^(1/2)보다 작거나 같다!
그래서 for문에서 순회하는 값의 범위가 2~루트N 까지로 해서 시간을 줄인 듯하다.
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(in.readLine());
for(int i=2;i<= Math.sqrt(N); i++) {
while(N%i == 0) {
System.out.println(i);
N /= i;
}
}
if(N != 1) {
System.out.println(N);
}
728x90
'알고리즘 > 백준' 카테고리의 다른 글
[문제] 백준 4948번 베트르랑 공준 - 소수 (0) | 2021.01.30 |
---|---|
[문제] 백준 1929번 소수 구하기 - 에라토스테네스의 체 이용하기 (0) | 2021.01.30 |
[문제] 백준 2581번 소수 (0) | 2021.01.30 |
[문제] 백준 2775번 부녀회장이 될테야 (0) | 2021.01.27 |
[문제] 백준 2839번 설탕 배달 (0) | 2021.01.25 |
Comments