Skip to content

Commit c84f8ec

Browse files
committed
[Gold V] Title: 같은 나머지, Time: 0 ms, Memory: 2156 KB -BaekjoonHub
1 parent 2e9d6d2 commit c84f8ec

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# [Gold V] 같은 나머지 - 1684
2+
3+
[문제 링크](https://www.acmicpc.net/problem/1684)
4+
5+
### 성능 요약
6+
7+
메모리: 2156 KB, 시간: 0 ms
8+
9+
### 분류
10+
11+
유클리드 호제법, 수학, 정수론
12+
13+
### 제출 일자
14+
15+
2024년 3월 25일 17:31:56
16+
17+
### 문제 설명
18+
19+
<p>정수 N을 정수 D로 나눴을 때의 몫을 Q, 나머지를 R이라고 하면 항등식 R = N - Q×D가 성립한다.</p>
20+
21+
<p>n개의 정수로 된 수열이 있을 때, 모든 정수를 한 정수 D로 나눴을 때 나머지가 같아지는 경우가 있다. 그리고 수열에 따라서는 이러한 정수 D가 여러 개 존재할 수 있다.</p>
22+
23+
<p>n개의 정수로 된 수열이 주어졌을 때, 가장 큰 D를 구하는 프로그램을 작성하시오.</p>
24+
25+
### 입력
26+
27+
<p>첫째 줄에 n(1 ≤ n ≤ 1,000)이 주어진다. 다음 줄에는 절댓값이 1,000,000을 넘지 않는 n개의 정수들이 주어진다.</p>
28+
29+
### 출력
30+
31+
<p>첫째 줄에 가장 큰 D를 출력한다. 항상 가장 큰 D가 존재하는 경우만 입력으로 주어진다.</p>
32+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <iostream>
2+
#include <algorithm>
3+
#include <vector>
4+
using namespace std;
5+
6+
int gcd(int a, int b) {
7+
while(b) {
8+
int c = a % b;
9+
a = b;
10+
b = c;
11+
}
12+
return a;
13+
}
14+
15+
int main()
16+
{
17+
ios_base::sync_with_stdio(0);
18+
cin.tie(0);
19+
//freopen("input.txt", "r", stdin);
20+
21+
int n, sum = 0;
22+
23+
cin >> n;
24+
vector<int> arr(n,0);
25+
26+
for (int i = 0; i < n; i++) {
27+
cin >> arr[i];
28+
}
29+
sort(arr.begin(), arr.end());
30+
31+
vector<int> subs;
32+
for (int i = 1; i < n; i++) {
33+
subs.push_back(arr[i] - arr[i-1]);
34+
}
35+
36+
int result = subs[0];
37+
for (int i = 1; i < n-1; i++) {
38+
result = gcd(subs[i], result);
39+
}
40+
41+
cout << result;
42+
return 0;
43+
}

0 commit comments

Comments
 (0)