美團點評2017秋招筆試編程題


https://www.nowcoder.com/test/5583018/summary

這套題目難度不大,主要是考了遞歸調用、完全背包、棧、字典樹

看得出是數據結構的題目多一些,最開始第二個題目,一個完全背包的,沒看出來是完全背包,感覺數據量不大,遞歸可以求吧

結果說數字大一點算不出答案,后來改循環,發現還是很多算不出,然后做別的題目去了,最后才做這個,才發現是個裸的完全背包。

第一題:用骰子來控制走的步數,然后給你一個總的步數,求走法有多少種

思路:遞歸分解即可

 1 import java.util.*;
 2  
 3 public class Main {
 4     public static void main(String[] args) {
 5         Scanner cin = new Scanner(System.in);
 6         int x = cin.nextInt();
 7         System.out.println(deal(x));
 8     }
 9     static int deal(int x){
10         if(x==0)
11             return 1;
12         int num = 0;
13         for(int i = 1;i<=6;i++){
14             if(x-i>=0){
15                 num+=deal(x-i);
16             }else
17                 return num;
18         }
19         return num;
20     }
21 }

 

第二題:給你六種紙幣(1,5,10,20,50,100),每種紙幣有無限多個,求N元錢用這六種紙幣組合可以有多少種組合方法

思路:完全背包,把紙幣的數量看成是重量,然后N元錢看成背包大小即可

 1 import java.util.*;
 2 
 3 public class Main {
 4     public static void main(String[] args) {
 5         Scanner cin = new Scanner(System.in);
 6         int weight[] = {1,5,10,20,50,100};
 7         Long dp[][] = new Long[10][10005];
 8         int n = cin.nextInt();
 9         for(int i = 0;i<dp.length;i++)
10             for(int j = 0;j<dp[i].length;j++)
11                 dp[i][j] = 0L;
12         dp[0][0] = 1L;
13         for(int i = 1;i<=weight.length;i++){
14             for(int j = 0;j<=n;j++){
15                 if(j< weight[i-1])
16                     dp[i][j] = dp[i-1][j];
17                 else
18                     dp[i][j] = dp[i-1][j]+dp[i][j-weight[i-1]];
19             }
20         }
21         System.out.println(dp[weight.length][n]);
22     }
23 }

 

第三題:和POJ這個題目一樣http://www.cnblogs.com/Tree-dream/p/5701137.html

 

第四題:給出兩個字符串(可能包含空格),找出其中最長的公共連續子串,輸出其長度

思路:構建字典樹進行比較即可

 1 import java.util.*;
 2  
 3 public class Main {
 4     public static void main(String[] args) {
 5         Scanner cin = new Scanner(System.in);
 6         String str = cin.nextLine();
 7         String p = cin.nextLine();
 8         Node root = new Node();
 9         for(int i = 0;i<str.length();i++){
10             if(root.next[str.charAt(i)]==null){
11                 root.next[str.charAt(i)] = new Node(str.charAt(i));
12                 add(root.next[str.charAt(i)],str.substring(i+1));
13             }else {
14                 add(root.next[str.charAt(i)],str.substring(i+1));
15             }
16         }
17         int max = 0;
18         for(int i = 0;i<p.length();i++){
19             if(root.next[p.charAt(i)]!=null){
20                 int tmp = Find(root,p.substring(i));
21                 if(tmp>max)
22                     max = tmp;
23             }
24         }
25         System.out.println(max);
26     }
27  
28     static int Find(Node root,String x){
29         Node tmp = root;
30         int leng = 0;
31         for(int i = 0;i<x.length();i++){
32             if(tmp.next[x.charAt(i)]!=null){
33                 leng++;
34                 tmp = tmp.next[x.charAt(i)];
35             }else
36                 break;
37         }
38         return leng;
39     }
40  
41     static void add(Node node,String x){
42         Node tmp = node;
43         for(int i = 0;i<x.length();i++){
44             if(tmp.next[x.charAt(i)]==null)
45                 tmp.next[x.charAt(i)] = new Node(x.charAt(i));
46             tmp = tmp.next[x.charAt(i)];
47         }
48     }
49 }
50 class Node{
51     Node next[] = new Node[200];
52     char str;
53     Node(char x){
54         this.str = x;
55         for(int i = 0;i<100;i++)
56             next[i] = null;
57     }
58     Node(){
59  
60     }
61 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM