숫자의 표현

BioBeBE·2022년 9월 13일
0

프로그래머스

목록 보기
40/40

문제풀이

삽입 정렬 역순으로, 본인 숫자는 안세므로 answer = 1 부터

코드

class Solution {
    public int solution(int n) {
        int answer = 1;
        int[] arr =new int[n+1];
        for(int i=1;i<=n;i++)arr[i]=i;
        for(int i=n;i>0;i--){
            int sum = 0;
            for(int j=i-1;j>0;j--){
                sum+=arr[j];
                if(sum==n){
                    answer++;
                    break;
                }else if(sum>n){
                    break;
                }
            }
        }
        return answer;
    }
}
profile
개발자지망생

0개의 댓글