今天开始学Java 美团编程题——大富翁游戏

大富翁游戏,玩家根据骰子的点数决定走的步数,即骰子点数为1时可以走一步,点数为2时可以走两步,点数为n时可以走n步。求玩家走到第n步(n<=骰子最大点数且是方法的唯一入参)时,总共有多少种投骰子的方法。 
输入描述:
输入包括一个整数n,(1 ≤ n ≤ 6)


输出描述:
输出一个整数,表示投骰子的方法

输入例子1:
6

输出例子1:
32

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(method(n));
        } 
    }
    public static int method(int n){
        int count = 0;
        if(n == 1 || n ==0){
            return 1;
        }
        if(n == 2){
             return 2;
        }else {
            //这里是个递归,f(n) = f(n-1)+f(n-2)+''''''+f(1)
            for(int i = 1;i<n;i++){
               count +=method(n-i); 
            }
            count++;
            return count;
        }
        
    }

猜你喜欢

转载自blog.csdn.net/u014566193/article/details/79646749