inblog logo
|
silver
    알고리즘문제풀기

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

    silver's avatar
    silver
    Feb 07, 2025
    [알고리즘문제풀기] 다음에 올 숫자
    Contents
    문제내가 작성한 오답내가 작성한 정답

    문제

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

    내가 작성한 오답

    : 런타임 에러 발생 → 정수 나눗셈으로 인한 오차 발생 가능성 존재 (등차 수열일 경우 : 결과가 소수점 이하가 존재하더라도 버림(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

    RSS·Powered by Inblog