大富翁游戏(跳台阶)&&拼凑钱币(求和的可能性)&&最大矩形面积&&最长公共子串

来源:互联网 发布:ubuntu touch命令 编辑:程序博客网 时间:2024/05/16 10:08

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

import java.util.Scanner;public class Main{    public static void main(String[] args){        Scanner sc = new Scanner(System.in);        int n = sc.nextInt();        Main ii = new Main();        int res = ii.result(n);        System.out.println(res);    }    public int result(int n){        if(n == 0){            return 0;        }        int[] dp = new int[n+1];        dp[0] = 1;        dp[1] = 1;        for(int i = 2;i<=n;i++){            dp[i] = 0;            for(int j = 0;j < i;j++) {                dp[i] += dp[j];            }        }        return dp[n];    }}
2、给你六种面额 1、5、10、20、50、100 元的纸币,假设每种币值的数量都足够多,编写程序求组成N元(N为0~10000的非负整数)的不同组合的个数。
/*动态规划 填表dp[h][n+1],h代表有h种硬币coins[]={1,5,10,20,50,100} ,           n+1代表要拼目标为0-n  递推公式 :dp[i][j]=dp[i][j]+dp[i-1][j-k*coins[i]],其中k[0,n/coins[i]].        解释:使用前i种钱币拼凑面值为j的方法数dp[i][j]=         使用前i-1种钱币,使用k个第i种钱币,拼凑面值为j的方法数,        即使用前i-1种钱币拼凑面值为j的方法数dp[i-1][j-k*coins[i]]初始化:上表的第一行dp[0]均为1,表示任意目标,只由面值为1的硬币拼凑,拼凑方法为1;*/import java.util.Scanner;import java.util.Arrays;public class Main{    public static long count(int n){    int coins[]={1,5,10,20,50,100};    int h=coins.length;    long dp[][]=new long[h][n+1];    Arrays.fill(dp[0], 1);//基础 该数字都由1组成 方法数是1    for(int i=1;i<h;i++){           for(int j=1;j<=n;j++){            int m=j/coins[i];          for(int k=0;k<=m;k++){                dp[i][j]=dp[i][j]+dp[i-1][j-k*coins[i]];            }           }         }    return dp[h-1][n];    }
    public static void main(String args[]){        Scanner sc=new Scanner(System.in);        while(sc.hasNext()){          int n=sc.nextInt();          long res=count(n);          System.out.println(res);        }    }}
3、给定一组非负整数组成的数组h,代表一组柱状图的高度,其中每个柱子的宽度都为1。 在这组柱状图中找到能组成的最大矩形的面积(如图所示)。 入参h为一个整型数组,代表每个柱子的高度,返回面积的值。

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();            int[] height = new int[n];            for(int i = 0; i < n; i++){                height[i] = sc.nextInt();            }            int MaxArea = 0;            int minH = 0;            for(int i = 0; i < n; i++){                minH = height[i];                for (int j = i; j < n; j++){                    minH = Math.min(minH, height[j]);                    MaxArea = Math.max(MaxArea, minH * (j - i + 1));                }            }             System.out.println(MaxArea);        }        sc.close();    }}
4、给出两个字符串(可能包含空格),找出其中最长的公共连续子串,输出其长度。

import java.util.Scanner; public class Main {    static String str1;    static String str2;    static int[][] dp;    static int Max;     public static void main(String[] args) {        // TODO Auto-generated method stub        Scanner in = new Scanner(System.in);        str1 = in.nextLine();        str2 = in.nextLine();        int len1 = str1.length();        int len2 = str2.length();        if ( len1 < 1 || len2 < 1){            System.out.print(0);        }         dp = new int[str1.length()+1][str2.length()+1];        char[] ch1 = str1.toCharArray();        char[] ch2 = str2.toCharArray();         for ( int i = 0 ; i < len1 ; i++){            if ( ch1[i] == ch2[0]){                dp[i][0] = 1;            }        }         for ( int i = 0 ; i < len2 ; i++){            if ( ch2[i] == ch1[0]){                dp[0][i] = 1;            }        }         Max = dp[0][0];        for ( int i = 1 ; i < len1 ; i++){            for ( int j = 1 ; j < len2 ; j++){                if ( ch1[i] == ch2[j]){                    dp[i][j] = dp[i-1][j-1]+1;                }                if ( dp[i][j] > Max){                    Max = dp[i][j];                }            }        }          System.out.print(Max);        in.close();    }}

阅读全文
2 0