프로그래머스/lv0
다음에 올 숫자
do_it0904
2022. 12. 6. 14:05
/* 다음에 올 숫자
* 등차수열 혹은 등비수열 common이 매개변수로 주어질 때,
* 마지막 원소 다음으로 올 숫자를 return
*
* common result
* [1, 2, 3, 4] 5
* [2, 4, 8] 16
*/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | public class programmer_0_2 { static int[] input1 = {1,2,3,4}; static int[] input2 = {2,4,8}; static int[] input3 = {-60,0,60}; public int solution(int[] common) { int answer = 0; int inputAr = common.length; if(common[1] - common[0] == common[2] - common[1]){ answer = common[common.length-1] + (common[1] - common[0]); } else answer = common[common.length-1] * (common[1]/common[0]); return answer; } public static void main(String[] args){ programmer_0_2 t = new programmer_0_2(); System.out.println("result = " + t.solution(input1)); System.out.println("---------------------------------------"); System.out.println("result2 = " + t.solution(input2)); System.out.println("---------------------------------------"); System.out.println("result3 = " + t.solution(input3)); System.out.println("---------------------------------------"); } } | cs |