大富翁游戏 -- JAVA 算法学习

由于正在找工作当中,所以就在牛客网上面找点题目做做看。

package com.yp;
import java.util.Scanner;
public class Dafuwen {
       private static int n ;
             public int game1(){
                  System. out .println( "大富翁游戏,玩家根据骰子的点数决定走的步数,即骰子点数为1时可以走一步,"
                              + "点数为2时可以走两步,点数为n时可以走n步。"
                              + "求玩家走到第n步(n<=骰子最大点数且是方法的唯一入参)时,总共有多少种投骰子的方法。" );
                  Scanner scanner = new Scanner(System. in );
                  System. out .println( "请输入要到达的位置需要的步数:" );
                   n = scanner .nextInt();
                   return ( int )Math.pow(2, n -1);
            }
             public int game2(){
                  System. out .println( "大富翁游戏,有一个六面的骰子(1-6),玩家根据骰子的点数决定走的步数,即骰子点数为1时可以走一步,"
                              + "点数为2时可以走两步,点数为n时可以走n步。"
                              + "求玩家走到第n步(n<=骰子最大点数且是方法的唯一入参)时,总共有多少种投骰子的方法。" );
                  Scanner scanner = new Scanner(System. in );
                  System. out .println( "请输入要到达的位置需要的步数:" );
                   n = scanner .nextInt();
                   if ( n <=6)
                   return ( int )Math.pow(2, n -1);
                   if ( n ==7)
                   return ( int )Math.pow(2, n -1)-1;
                   return ( int )Math.pow(2, n -1)-( n -6)*( n -6)+1;
            }
       public static void main(String[] args ) {
            
            Dafuwen dafuwen = new Dafuwen();
            System. out .println( dafuwen .game1());
            System. out .println( dafuwen .game2());
      }
}

版权声明:本文为github_39500681原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。