[프로그래머스/C++]Lv.0 - 다음에 올 숫자

YH J·2023년 4월 17일
0

프로그래머스

목록 보기
1/168

문제 링크

https://school.programmers.co.kr/learn/courses/30/lessons/120924

내 풀이

등차 혹은 등비 수열이므로 연속되는 3개의 원소값을 비교하여 등비인지 등차인지 알아낸 후 계산

내 코드

#include <string>
#include <vector>

using namespace std;

int solution(vector<int> common) {
    
    int a = common[common.size()-1];
    int b = common[common.size()-2];
    int c = common[common.size()-3];
    
    if (a-b == b-c)
        a += a-b;
    else if(a/b == b/c)
        a *= a/b;
    
    return a;
}

다른 사람의 풀이

#include <string>
#include <vector>

using namespace std;

int solution(vector<int> common) {
    int answer = 0;
    if(common[0] - common[1] == common[1] - common[2]){
        return common.back() - (common[0] - common[1]);
    }
    else{
        return common.back() * (common[1] / common[0]);
    }
    return answer;
}

다른 사람의 풀이 해석

나는 뒤에서 3개를 비교했고 이 풀이는 앞의 3개를 비교하였다.

profile
게임 개발자 지망생

0개의 댓글