[알고리즘문제풀기] 다음에 올 숫자

silver's avatar
Feb 07, 2025
[알고리즘문제풀기] 다음에 올 숫자

문제

내가 작성한 오답

: 런타임 에러 발생 → 정수 나눗셈으로 인한 오차 발생 가능성 존재 (등차 수열일 경우 : 결과가 소수점 이하가 존재하더라도 버림(truncate)되어 정수가 된다.)
notion image
class Solution { public int solution(int[] common) { int answer = 0; 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; } }

내가 작성한 정답

: 등비수열일 경우는 나눌 경우 주어진 조건에 의해 정수만 나오기 때문에 if절에서 등차수열부터 걸러낸다.
class Solution { public int solution(int[] common) { int answer = 0; 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; } }
Share article

silver