[Programming Question] Monopoly Game (Meituan Review 2017 Autumn Recruitment)

[Programming question] Monopoly game

Time limit: 1 second

Space limit: 32768K

In the Monopoly game, the player decides the number of steps to take according to the number of points on the dice, that is, when the point of the dice is 1, he can take one step, when the point is 2, he can take two steps, and when the point is n, he can take n steps. Find the total number of ways to roll the dice when the player reaches the nth step (n<=the maximum number of dice points and is the only input parameter of the method).

  • Enter description:

The input consists of an integer n, (1 ≤ n ≤ 6)

  • Output description:

Output an integer representing the method of throwing the dice

  • Input example 1:

6

  • Output example 1:

32

  • code
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()) {
            int n = sc.nextInt();
            System.out.println(fun(n));
        }
    }
    public static int fun(int n){
        int count = 0;
        if(n == 1) {
            count = 1;
        }else if(n == 2) {
            count = 2;
        }else {
            for(int i = 1; i < n; i++) {
                count += fun(n - i);
            }
            count += 1;
        }
        return count;
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325817884&siteId=291194637