AI로 러닝(Learn) 내일을 향해 러닝(Running)

원당컴퓨터학원에서 배우는 AI, 세상을 향해 달리다

자격증/COS Pro

COSPro 2급] 2차 문제6) 엘리베이터의 총 이동거리구하기

파아란기쁨1 2022. 7. 12. 19:15
반응형

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int solution(vector<int> floors) {
    int dist = 0;
    int length = floors.size();
    for(int i = 1; i<length; ++i){
        if(floors[i]>floors[i-1])
            dist += floors[i] - floors[i-1];
        else
            dist += floors[i-1] - floors[i];
    }
    return dist;
}

// 아래는 테스트케이스 출력을 해보기 위한 main 함수입니다.
int main() {
    vector<int> floors = {1, 2, 5, 4, 2};
    int ret = solution(floors);

    // [실행] 버튼을 누르면 출력 값을 볼 수 있습니다.
    cout << "solution 함수의 반환 값은 " << ret << " 입니다." << endl;
}
반응형