Skip to content

Commit 2e65818

Browse files
committed
[Silver II] Title: 숨바꼭질 6, Time: 320 ms, Memory: 26196 KB -BaekjoonHub
1 parent ad0e0eb commit 2e65818

2 files changed

Lines changed: 16 additions & 18 deletions

File tree

백준/Silver/17087. 숨바꼭질 6/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
### 성능 요약
66

7-
메모리: 26068 KB, 시간: 1168 ms
7+
메모리: 26196 KB, 시간: 320 ms
88

99
### 분류
1010

1111
수학, 정수론, 유클리드 호제법
1212

1313
### 제출 일자
1414

15-
2025년 8월 29일 14:13:52
15+
2025년 8월 29일 14:16:31
1616

1717
### 문제 설명
1818

백준/Silver/17087. 숨바꼭질 6/숨바꼭질 6.java

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,30 @@ public static void main(String[] args) throws IOException {
1212
int S = Integer.parseInt(st.nextToken());
1313
int[] arr = new int[N];
1414
st = new StringTokenizer(br.readLine());
15-
int min = 1000000001;
1615
for (int i = 0; i <N ; i++) {
1716
int tmp =Math.abs(Integer.parseInt(st.nextToken()) - S);
18-
if (tmp<min){
19-
min = tmp;
20-
}
2117
arr[i] = Math.abs(tmp);
2218

2319
}
2420

25-
System.out.println(GCD(arr,min));
21+
System.out.println(GCD(arr));
2622

2723
}
2824

29-
private static int GCD(int[] arr, int min) {
30-
for (int i = min; i >0 ; i--) {
31-
boolean ok = true;
32-
for (int x : arr) {
33-
if (x % i != 0) {
34-
ok = false;
35-
break;
36-
}
37-
}
38-
if (ok) return i;
25+
private static int gcd(int a, int b) {
26+
while (b != 0) {
27+
int tmp = a % b;
28+
a = b;
29+
b = tmp;
30+
}
31+
return a;
32+
}
3933

34+
private static int GCD(int[] arr) {
35+
int g = arr[0];
36+
for (int i = 1; i < arr.length; i++) {
37+
g = gcd(g, arr[i]);
4038
}
41-
return 1;
39+
return g;
4240
}
4341
}

0 commit comments

Comments
 (0)