728x90
반응형
이 문제는 유클리드 호제법으로 푸는 문제이다.
모든 동생을 찾기 위한 D의 값을 정하기 위해 최대 공약수를 구하는 문제이다.
동생의 위치에서 수빈이의 위치를 빼고 절댓값을 씌운 후 그 값들을 유클리드 함수에 집어 넣어
최대 공약수를 구한다.
//숨바꼭질 6
#include <iostream>
#include <vector>
using namespace std;
int uclid(int a, int b){
while(a % b != 0){
int temp = b;
b = a % b;
a = temp;
}
return b;
}
int main(void){
int n, s, num, gcd;
vector<int> sisterVector;
cin >> n >> s;
for(int i = 0; i < n; i++){
cin >> num;
sisterVector.push_back(abs(num - s));
}
gcd = sisterVector[0];
for(int i = 1; i < n; i++){
gcd = uclid(gcd, sisterVector[i]);
}
cout << gcd;
}
반응형
'algorithm' 카테고리의 다른 글
[algorithm] 백준 - 외판원 순회 2 (0) | 2020.11.06 |
---|---|
[algorithm] 백준 - 숨바꼭질 4 (0) | 2020.11.05 |
[algorithm] 백준 - 조합 0의 개수 (0) | 2020.11.04 |
[algorithm] 백준 - 리모컨 (0) | 2020.11.04 |
[algorithm] 백준 - 수 이어 쓰기 1 (0) | 2020.11.03 |