Algorithm/Programmers
[Algorithm] 점프뛰기 (JumpCase)
Lim-Ky
2017. 10. 5. 16:17
반응형
Programmers Level_03
점프뛰기 (JumpCase)
효진이는 멀리 뛰기를 연습하고 있습니다. 효진이는 한번에 1칸, 또는 2칸을 뛸 수 있습니다. 칸이 총 4개 있을 때, 효진이는
(1칸, 1칸, 1칸, 1칸)
(1칸, 2칸, 1칸)
(1칸, 1칸, 2칸)
(2칸, 1칸, 1칸)
(2칸, 2칸)
의 5가지 방법으로 맨 끝 칸에 도달할 수 있습니다. 멀리뛰기에 사용될 칸의 수 n이 주어질 때, 효진이가 끝에 도달하는 방법이 몇 가지인지 출력하는 jumpCase 함수를 완성하세요. 예를 들어 4가 입력된다면, 5를 반환해 주면 됩니다.
package Programmers_Level03; public class JumpCase { public static void main(String[] args) { JumpCase c = new JumpCase(); int testCase =8; //아래는 테스트로 출력해 보기 위한 코드입니다. System.out.println(c.jumpCase(testCase)); } public int jumpCase(int num) { double answer = 0; int n = (num/2)+1; // System.out.println(n); for(int i=0; i<n; i++){ int oneNum = 0; int two = 2*i; int remain = num-two; while(remain > 0){ remain -= 1; oneNum++; } // System.out.println("2 : "+i +" 1 : "+oneNum); double a =factorial(i+oneNum); double b =factorial(i); double c =factorial(oneNum); answer += a/(b*c); } return (int) answer; } public static Double factorial(int n) { Double fac=(double) 1; for(int i=n; i>=1; i--) { fac=fac*i; } return fac; } }
반응형