diff --git a/code_study/programmers/다음에 올 숫자/problem.txt b/code_study/programmers/다음에 올 숫자/problem.txt new file mode 100644 index 0000000..768d482 --- /dev/null +++ b/code_study/programmers/다음에 올 숫자/problem.txt @@ -0,0 +1,14 @@ +등차수열 혹은 등비수열 common이 매개변수로 주어질 때, +마지막 원소 다음으로 올 숫자를 return 하도록 solution 함수를 완성해보세요. + +제한사항 +2 < common의 길이 < 1,000 +-1,000 < common의 원소 < 2,000 +common의 원소는 모두 정수입니다. +등차수열 혹은 등비수열이 아닌 경우는 없습니다. +등비수열인 경우 공비는 0이 아닌 정수입니다. + +입출력 예 +common result +[1, 2, 3, 4] 5 +[2, 4, 8] 16 diff --git a/code_study/programmers/다음에 올 숫자/solution.js b/code_study/programmers/다음에 올 숫자/solution.js new file mode 100644 index 0000000..f9c4340 --- /dev/null +++ b/code_study/programmers/다음에 올 숫자/solution.js @@ -0,0 +1,10 @@ +function solution(common) { + var _a = [common[0], common[1], common[2]], a = _a[0], b = _a[1], c = _a[2]; + var len = common.length; + if (a + c === 2 * b) + return common[len - 1] + (b - a); + else + return common[len - 1] * (b / a); +} +var common = [1, 2, 3, 4]; +console.log(solution(common)); diff --git a/code_study/programmers/다음에 올 숫자/solution.ts b/code_study/programmers/다음에 올 숫자/solution.ts new file mode 100644 index 0000000..22b1e5e --- /dev/null +++ b/code_study/programmers/다음에 올 숫자/solution.ts @@ -0,0 +1,10 @@ +function solution(common: number[]) { + let [a, b, c]: number[] = [common[0], common[1], common[2]]; + let len: number = common.length; + + if(a + c === 2 * b) return common[len - 1] + (b - a); + else return common[len - 1] * (b / a); +} + +let common: number[] = [1,2,3,4]; +console.log(solution(common)); \ No newline at end of file