BOJ 2485 가로수

1 개요[ | ]

BOJ 2485 가로수


2 C++[ | ]

#include <bits/stdc++.h>
using namespace std;

int N, i, temp, previous, diff, commonDiff, nums[100000];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    cin >> N;
    previous = -1;
    commonDiff = -1;
    for(i=0; i<N; i++) {
        cin >> temp;
        nums[i] = temp;
        if( previous > 0 ) {
            diff = temp - previous;
            if( commonDiff < 0 ) commonDiff = diff;
            else commonDiff = gcd(diff, commonDiff);
        }
        previous = temp;
    }
    int pos = nums[0];
    int need = 0;
    for(i=0; i<N-1; i++) {
        need--;
        for(pos=nums[i]; pos<nums[i+1]; pos+=commonDiff) {
            need++;
        }
    }
    cout << need;
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}