美团编程题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiao__jia__jia/article/details/82456645



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

输入描述:

输入包括一个整数n,(1 ≤ n ≤ 6)

输出描述:

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

输入例子1:

6

输出例子1:

32

题解:递归
import java.util.Scanner;
public class Main {
    static int sum = 0;
    public static void main(String[] args) {
        Scanner in  = new Scanner(System.in);
        int n = in.nextInt();
        dp(n,0);
        System.out.println(sum);
    }
    public static void  dp(int n, int cnt) {
          if( n < cnt){
              return ;
          }
          if(n == cnt) {
              sum++;
          }
         for(int i = 1; i <= n; i++) {
            dp(n, cnt + i);
        }
        
    }
}

                                                                                  拼凑钱币

给你六种面额 1、5、10、20、50、100 元的纸币,假设每种币值的数量都足够多,编写程序求组成N元(N为0~10000的非负整数)的不同组合的个数。 

输入描述:

输入包括一个整数n(1 ≤ n ≤ 10000)

输出描述:

输出一个整数,表示不同的组合方案数

输入例子1:

1

输出例子1:

1


 

#include<iostream>
using namespace std;
  
int main(){
    int N = 0;
    cin >> N;
    long long * F = new long long[N + 1]();
    int c[6] = { 1, 5, 10, 20, 50, 100 };
  
    F[0] = 1;
    for (int i = 0; i < 6; i++)
        for (int v = c[i]; v <= N; v++){
            F[v] = F[v] + F[v - c[i]];
        }
    cout << F[N]<<endl;
    return 0;
}


给定一组非负整数组成的数组h,代表一组柱状图的高度,其中每个柱子的宽度都为1。 在这组柱状图中找到能组成的最大矩形的面积(如图所示)。 入参h为一个整型数组,代表每个柱子的高度,返回面积的值。



 

输入描述:

输入包括两行,第一行包含一个整数n(1 ≤ n ≤ 10000)
第二行包括n个整数,表示h数组中的每个值,h_i(1 ≤ h_i ≤ 1,000,000)


 

输出描述:

输出一个整数,表示最大的矩阵面积。

示例1

输入

6
2 1 5 6 2 3

输出

10

解析:还得考虑单条最大问题


importjava.util.*;
publicclassMain{ 
publicstaticvoidmain(String args[]){          
              Scanner sc=newScanner(System.in);
              int n =  sc.nextInt();
              int[] array = newint[n];
              for(inti=0;i<n;i++)
              {
                  array[i] =  sc.nextInt();
              }         
              int result = 0;       
              for(inti =0;i<n;i++)
              {   int max = array[i];
                  int minheight = array[i];
                  for(intj=(i+1);j<n;j++)
                  {
                     minheight =  Math.min(minheight,array[j]);            
                     max = Math.max(max, (j-i+1)*minheight);
                     result =  Math.max(result,max);
                  }
              }
              System.out.println(result);
          }
      }




最长公共连续子串
给出两个字符串(可能包含空格),找出其中最长的公共连续子串,输出其长度。

输入描述:

输入为两行字符串(可能包含空格),长度均小于等于50.

输出描述:

输出为一个整数,表示最长公共连续子串的长度。

示例1

输入

abcde
abgde

输出

2


思路:动态规划dp:公共子串

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        String str1 = in.nextLine();
        System.out.println(lcs(str,str1));
    }
    public static int lcs(String str1, String str2) { 
    int len1 = str1.length(); 
    int len2 = str2.length(); 
    int result = 0;
    int c[][] = new int[len1+1][len2+1]; 
    for (int i = 0; i <= len1; i++) { 
        for( int j = 0; j <= len2; j++) { 
            if(i == 0 || j == 0) { 
                c[i][j] = 0; 
            } else if (str1.charAt(i-1) == str2.charAt(j-1)) { 
                c[i][j] = c[i-1][j-1] + 1; 
                result = Math.max(c[i][j], result); 
            } else { 
                c[i][j] = 0; 
            } 
        } 
    } 
    return result; 
} 
}

猜你喜欢

转载自blog.csdn.net/xiao__jia__jia/article/details/82456645