面试-机试-编程题--剑指offer

来源:互联网 发布:淘宝上专卖点怎么申请 编辑:程序博客网 时间:2024/06/05 06:48

如果要面试java,最好要看看http://www.cnblogs.com/lanhj/p/4672735.html 概括了所有可能问到的java问题

1 360 内推笔试 

这个题目涉及到用list存数组,map取值,还有就是怎么才能根据控制台中输入的是几行的数据,再回车然后继续往下程序运行,主要是用了for循环。


以下是代码部分:

[java] view plain copy
  1. import java.io.BufferedReader;  
  2. import java.io.IOException;  
  3. import java.io.InputStreamReader;  
  4. import java.util.ArrayList;  
  5. import java.util.HashMap;  
  6. import java.util.List;  
  7. import java.util.Map;  
  8. public class Test{  
  9.     public static void main(String args[]){  
  10.         int count = Integer.parseInt(getInput());  
  11.         List<String> list = new ArrayList<String>();//主要引入的包一定要是util.List.如果是awt.list容易有错误  
  12.         for(int i =0;i < count;i++){   //注意这个地方实现的是,可以读取到几行数据,然后根据输入的int来去判断,当没有输入count行,程序会在这个地方等待输入。  
  13.             list.add(getInput());  
  14.         }  
  15.         //System.out.println(count);  
  16.         for(int i=0;i < count;i++){  
  17.             //System.out.println("list"+ list.get(i));  
  18.             char flag = getFirstNonChar(list.get(i));  
  19.             if(flag == '0'){  
  20.                 System.out.println("no");  
  21.             }  
  22.             else{  
  23.                  System.out.println(flag);  
  24.             }  
  25.              
  26.         }         
  27.     }  
  28.     /*一般都使用entry来遍历整个map: 
  29.     Map<Integer, Integer> map = new HashMap<Integer, Integer>();   
  30.        
  31.     for (Map.Entry<Integer, Integer> entry : map.entrySet()) {   
  32.        
  33.         System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());   
  34.        
  35.     }  */  
  36.     public static char getFirstNonChar(String str){  
  37.         Map<Character, Integer> map = new HashMap<Character, Integer>(str.length());//我刚开始这个str.length没有加进去,然后如果输入ha的话则输出是a  
  38.         int flag = 0;  
  39.         for(char c:str.toCharArray()){  
  40.             map.put(c, map.containsKey(c) ? map.get(c) + 1 : 1);//这个地方写的非常棒。  
  41.         }  
  42.         for(Map.Entry<Character, Integer> entry : map.entrySet()){  
  43.             if(entry.getValue() == 1){  
  44.                 //System.out.println(entry.getKey());  
  45.                 return entry.getKey();  
  46.             }  
  47.         }  
  48.         //throw new RuntimeException("Can\'t find any non repeated Character");  
  49.         return '0';  
  50.     }  
  51.     public static String getInput(){  
  52.         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));  
  53.         String s1 = "";  
  54.         try {  
  55.             s1 = in.readLine();  
  56.         } catch (IOException e) {  
  57.             // TODO Auto-generated catch block  
  58.             e.printStackTrace();  
  59.         }  
  60.         return s1;  
  61.     }  
  62. }  

2 KMP算法

 关于该算法的分析,请看http://www.cnblogs.com/maybe2030/p/4633153.html 

代码实现部分,其实主要是next数组的获取:
算法的关键在于next数组的生成,用动态规范法生成。
比如:str = abcabd,next初始化为:[0,0,0,0,0,0];
已知第0个字符a没有任何相同的前后缀,则next[0] = 0。
加入第1个字符,则前面已知的最长公共前后缀长度为next[0],此时如果str[next[0]]与str[1]相等,就可知道next[1]=next[0]+1,如不相等则可直接判定next[1]=0;这里str[next[0]]!=str[1],故next[1]=0。
...,next[2]=0。
...,next[3]=next[2]+1=1。
...,next[4]=next[3]+1=2。
...,next[5]=0。
最后,next=[0,0,0,1,2,0];

代码附上,其实我也没有看懂,知道大概的思路就可以了:

[java] view plain copy
  1. package com.shu.frnak;  
  2.   
  3. public class KMP {    
  4.         
  5.     void getNext(String pattern, int next[]) { //把匹配值算出来放到 next[]数组中  
  6.         int j = 0;    
  7.         int k = -1;    
  8.         int len = pattern.length();    
  9.         next[0] = -1;    
  10.     
  11.         while (j < len - 1) {    
  12.             if (k == -1 || pattern.charAt(k) == pattern.charAt(j)) {    
  13.     
  14.                 j++;    
  15.                 k++;    
  16.                 next[j] = k;    
  17.             } else {    
  18.     
  19.                 // 比较到第K个字符,说明p[0——k-1]字符串和p[j-k——j-1]字符串相等,而next[k]表示    
  20.                 // p[0——k-1]的前缀和后缀的最长共有长度,所接下来可以直接比较p[next[k]]和p[j]    
  21.                 k = next[k];    
  22.             }    
  23.         }    
  24.     
  25.     }    
  26.     
  27.     int kmp(String s, String pattern) {    
  28.         int i = 0;    
  29.         int j = 0;    
  30.         int slen = s.length();    
  31.         int plen = pattern.length();    
  32.     
  33.         int[] next = new int[plen];    
  34.     
  35.         getNext(pattern, next);    
  36.     
  37.         while (i < slen && j < plen) {    
  38.     
  39.             if (s.charAt(i) == pattern.charAt(j)) {    
  40.                 i++;    
  41.                 j++;    
  42.             } else {    
  43.                 if (next[j] == -1) {    
  44.                     i++;    
  45.                     j = 0;    
  46.                 } else {    
  47.                     j = next[j];    
  48.                 }    
  49.     
  50.             }    
  51.     
  52.             if (j == plen) {    
  53.                 return i - j;    
  54.             }    
  55.         }    
  56.         return -1;    
  57.     }    
  58.     
  59.     /**  
  60.      * @param args  
  61.      */    
  62.     public static void main(String[] args) {    
  63.         // TODO Auto-generated method stub    
  64.         KMP kmp = new KMP();    
  65.         String str = "bbc abcdab abcdabcdabde";    
  66.         String pattern = "abcdabd";    
  67.         System.out.println(kmp.kmp(str, pattern));    
  68.     }    
  69.     
  70. }    

3 阿里测试开发笔试题目



这类题网上有算法差不多的。主要采用递归回溯的方法:

[java] view plain copy
  1. package com.shu.frnak;  
  2. import java.util.HashSet;  
  3. import java.util.Set;  
  4.   
  5. public class TestList {     
  6.       
  7.     String partString(String input, Set<String> dict){  
  8.         if(dict.contains(input))  
  9.             return input;  
  10.         int length = input.length();  
  11.         for(int i =1;i<length;i++){  
  12.             String prefix = input.substring(0, i);  
  13.             if(dict.contains(prefix)){  
  14.                 String suffix = input.substring(i, length);  
  15.                 String segSuffix = partString(suffix, dict);  
  16.                 if (segSuffix != null) {  
  17.                     return prefix + " " + segSuffix;  
  18.                 }  
  19.             }  
  20.         }  
  21.         return null;  
  22.     }  
  23.     String testString(String srcinput,String rightinput,Set<String> dict){  
  24.         String result = partString(srcinput, dict);  
  25.         if(result.equals(rightinput)){  
  26.             return "true";  
  27.         }  
  28.         else{  
  29.             return "false";  
  30.         }  
  31.     }  
  32.     public static void main(String[] args){  
  33.         TestList test = new TestList();  
  34.         Set<String> dict1 = new HashSet<String>();  
  35.         dict1.add("a");  
  36.         dict1.add("brown");  
  37.         dict1.add("fox");  
  38.         dict1.add("jumps");  
  39.         dict1.add("over");  
  40.         dict1.add("lazy");  
  41.         dict1.add("dog");  
  42.         //String result = test.partString("abrownfoxjumpsoveralazydog", dict1);  
  43.         String flags1 = test.testString("abrownfoxjumpsoveralazydog""a brown fox jumps over a lazy dog", dict1);  
  44.         System.out.println(flags1);  
  45.         String flags2 = test.testString("a""a", dict1);  
  46.         System.out.println(flags2);  
  47.           
  48.     }  
  49. }  

4 华为机试

注意输入输出的格式,在java中智能用next()或者nextInt()去获取输入的数字,不能用nextLine(),如果用nextLine就会报错误。



[java] view plain copy
  1. package com.shu.frank;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.Collections;  
  5. import java.util.Comparator;  
  6. import java.util.List;  
  7. import java.util.Map;  
  8. import java.util.Scanner;  
  9. import java.util.TreeMap;  
  10. import java.util.Map.Entry;  
  11. import java.util.regex.Matcher;  
  12. import java.util.regex.Pattern;  
  13.   
  14. public class Main {  
  15.     public static void main(String[] args) {  
  16.         // TODO Auto-generated method stub  
  17.         Scanner cin = new Scanner(System.in);  
  18.         List<String> list = new ArrayList<String>();  
  19.         Pattern votejudgPattern = Pattern.compile("vote");  
  20.         while(cin.hasNext()){  
  21.             String oneline = cin.nextLine();  
  22.             Matcher matcher = votejudgPattern.matcher(oneline);  
  23.                 if(matcher.find()){  
  24.                     list.add(oneline);  
  25.                 }  
  26.                 if((oneline.equals("getVoteResult"))){  
  27.                     int a=0;  
  28.                     int b=0;  
  29.                     int c=0;  
  30.                     int d=0;  
  31.                     int m=0;  
  32.                     for(int i=0;i<list.size();i++){  
  33.                         if(list.get(i).equals("vote A")){  
  34.                             ++a;  
  35.                         }  
  36.                         if(list.get(i).equals("vote B")){  
  37.                             ++b;  
  38.                         }  
  39.                         if(list.get(i).equals("vote C")){  
  40.                             ++c;  
  41.                         }  
  42.                         if(list.get(i).equals("vote D")){  
  43.                             ++d;  
  44.                         }  
  45.                         if(!((list.get(i).equals("vote A")) || (list.get(i).equals("vote B")) || (list.get(i).equals("vote C")) || (list.get(i).equals("vote D")))){  
  46.                             m++;  
  47.                             //System.out.println("½øÈë");  
  48.                         }  
  49.                     }  
  50.                     Map<String, Integer> map = new TreeMap<String, Integer>();  
  51.                     map.put("A", a);  
  52.                     map.put("B", b);  
  53.                     map.put("C", c);  
  54.                     map.put("D", d);  
  55.                     List<Map.Entry<String,Integer>> listsort = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());  
  56.                     Collections.sort(listsort, new Comparator<Map.Entry<String, Integer>>() {  
  57.                         public int compare(Entry<String, Integer>o1,Entry<String,Integer>o2){  
  58.                             return o2.getValue().compareTo(o1.getValue());  
  59.                         }  
  60.                     });  
  61.                     for(Map.Entry<String, Integer> mapping:listsort){  
  62.                         System.out.println(mapping.getKey()+" "+mapping.getValue());  
  63.                     }  
  64.                     System.out.println(m);  
  65.                     //System.out.println("list"+list.toString());  
  66.                     break;  
  67.                 }  
  68.         }  
  69.     }  
  70.   
  71. }  

总结TreeMap分别对Vaule和Key进行排序的方法:

[java] view plain copy
  1. TreeMap:用于排序  
  2. 1 TreeMap默认是根据Key进行升序排练的。  
  3.         //如果要改变排序方式,则需要使用比较器:Comparator,它可以对集合对象或者数组进行排序的比较接口  
  4. Map<String ,String> map = new TreeMap<String,String>(  
  5.                 new Comparator<String>() {  
  6.                     public int compare(String obj1,String obj2){  
  7.                         return obj2.compareTo(obj1);  
  8.                     }  
  9.                 }  
  10.         );  
  11.          map.put("b""ccccc");  
  12.          map.put("d""aaaaa");  
  13.          map.put("c""bbbbb");  
  14.          map.put("a""ddddd");  
  15.          //遍历整个map  
  16.          for(Map.Entry<String, String> mapsort:map.entrySet()){  
  17.              System.out.println(mapsort.getKey()+","+mapsort.getValue());  
  18.          }  
  19.  2 //TreeMap的value来进行排序。对value排序我们就需要借助于Collections的sort(List<T> list,  
  20.         //Comparator<? super T> c)方法,该方法根据指定比较器产生的顺序对指定列表进行排序。但是有一个前提条件,  
  21.         //那就是所有的元素都必须能够根据所提供的比较器来进行比较  
  22.         Map<String,String> map = new TreeMap<String, String>();  
  23.          map.put("a""ddddd");  
  24.          map.put("c""bbbbb");  
  25.          map.put("d""aaaaa");  
  26.          map.put("b""ccccc");  
  27.          List<Map.Entry<String, String>> list = new ArrayList<Map.Entry<String, String>>(map.entrySet());  
  28.          Collections.sort(list, new Comparator<Map.Entry<String, String>>() {  
  29.   
  30.             @Override  
  31.             public int compare(Entry<String, String> o1,  
  32.                     Entry<String, String> o2) {  
  33.                 // TODO Auto-generated method stub  
  34.                 return o1.getValue().compareTo(o2.getValue());  
  35.             }  
  36.          });  
  37.          for(Map.Entry<String, String> mapping:list){  
  38.              System.out.println(mapping.getKey()+":"+mapping.getValue());  
  39.          }  

5 华为机试,注意输入输出的格式


[java] view plain copy
  1. package com.shu.cisco;  
  2. import java.util.ArrayList;  
  3. import java.util.Collections;  
  4. import java.util.Comparator;  
  5. import java.util.Map;  
  6. import java.util.Map.Entry;  
  7. import java.util.Scanner;  
  8. import java.util.TreeMap;  
  9. public class Main {  
  10.     public Map<Character, Integer> getmapCharcount(String s){  
  11.         Map<Character, Integer> tree = new TreeMap<Character, Integer>();  
  12.         String sx = s.toLowerCase();  
  13.         //下面这个for循环是统计一个字符串中字符的出现次数,并且把字符和次数,存放到Map中,并且按照次数进行排序。  
  14.         for(int i=0;i<sx.length();i++){  
  15.             char ch = sx.charAt(i);  
  16.             if(ch >='a' && ch <= 'z'){  
  17.                 if(!tree.containsKey(ch)){  
  18.                     tree.put(ch, new Integer(1));  
  19.                 }  
  20.                 else{  
  21.                     Integer in = (Integer)tree.get(ch) + 1;//注意这个地方,一定要注意  
  22.                     tree.put(ch, in);  
  23.                 }  
  24.             }  
  25.         }  
  26.         ArrayList<Entry<Character, Integer>> list = new ArrayList<Map.Entry<Character, Integer>>(tree.entrySet());  
  27.         Collections.sort(list,new Comparator<Map.Entry<Character, Integer>>() {  
  28.   
  29.             @Override  
  30.             public int compare(Entry<Character, Integer> o1,  
  31.                     Entry<Character, Integer> o2) {  
  32.                 // TODO Auto-generated method stub  
  33.                 return o2.getValue().compareTo(o1.getValue());  
  34.             }  
  35.         });  
  36.         int q=26;  
  37.         int sum=0;  
  38.         for(Map.Entry<Character, Integer> mapsort:list){  
  39.             sum =sum+ mapsort.getValue() * (q--);  
  40.             //System.out.println(mapsort.getValue()*(q--));  
  41.         }  
  42.         System.out.println(sum);  
  43.         return null;  
  44.     }  
  45.     public static void main(String[] args) {  
  46.         Scanner cin = new Scanner(System.in);  
  47.         Main main = new Main();  
  48.         int count = cin.nextInt();  
  49.         String aa = "";  
  50.         for(int i=0;i<count;i++){  
  51.             aa = aa + cin.next()+" ";  
  52.         }  
  53.         String[] split = aa.split(" ");  
  54.         for(int q=0;q<split.length;q++){  
  55.             main.getmapCharcount(split[q]);  
  56.             //System.out.println(split[q]);  
  57.         }  
  58.     }  
  59. }  

6 对数组进行排序问题


主要使用的是Arrays的sort()方法,注意:Arrays的常用方法还有Arrays.asList(List),返回一个固定长度大小的list,还有binarySearch(char[] a,char key)

a - 要搜索的数组
key - 要搜索的值  ,如果没有找到则返回0

[java] view plain copy
  1. public class Main {  
  2.     public static void main(String[] args) {  
  3.         Scanner cin = new Scanner(System.in);  
  4.         Main main = new Main();  
  5.         char[] array = cin.nextLine().toCharArray();  
  6.         Arrays.sort(array);  
  7.         System.out.println(array);  
  8.     }  
  9. }  

7 华为机试 判断ip以及子网掩码的合法性

这个题目的合法性判断主要是:子网掩码第一部分应为255,第二三部分为0或者255,第四部分为0;IP地址的是个部分均在0到255之间。


[java] view plain copy
  1. package com.shu.cisco;  
  2.   
  3. import java.util.Scanner;  
  4.   
  5. public class Main {  
  6.       
  7.     public int checkNetSegment(String mask, String ip1, String ip2)   
  8.     {       
  9.         //子网掩码第一部分应为255,第二三部分为0或者255,第四部分为0;IP地址的是个部分均在0到255之间。  
  10.         int flags=3;  
  11.         String[] maskArray = mask.split("\\.");  
  12.         String[] ip1Array = ip1.split("\\.");  
  13.         String[] ip2Array = ip2.split("\\.");  
  14.         int[] maskint = new int[mask.length()];  
  15.         int[] ip1int = new int[ip1.length()];  
  16.         int[] ip2int = new int[ip2.length()];  
  17.         int[] ip1result = new int[4];  
  18.         int[] ip2result = new int[4];  
  19.         if(!((maskArray[0].equals("255")) && (maskArray[3].equals("0")) && ((maskArray[1].equals("255"))||(maskArray[1].equals("0"))) && ((maskArray[2].equals("255"))||(maskArray[2].equals("0"))))){  
  20.             System.out.println("mask不合法");  
  21.             return flags=1;  
  22.         }  
  23.         for(int i=0;i<maskArray.length;i++){  
  24.             maskint[i]=Integer.parseInt(maskArray[i]);  
  25.         }  
  26.         for(int i=0;i<ip1Array.length;i++){  
  27.             ip1int[i]=Integer.parseInt(ip1Array[i]);  
  28.             if((ip1int[i]<0)||ip1int[i]>255){  
  29.                 //System.out.println("ip1不合法");  
  30.                 return flags = 1;  
  31.             }  
  32.             ip1result[i] = ip1int[i] & maskint[i];  
  33.               
  34.         }  
  35.         for(int i=0;i<ip2Array.length;i++){  
  36.             ip2int[i]=Integer.parseInt(ip2Array[i]);  
  37.             if((ip2int[i]<0)||ip2int[i]>255){  
  38.                 //System.out.println("ip2不合法");  
  39.                 return flags = 1;  
  40.             }  
  41.             ip2result[i] = ip2int[i] & maskint[i];  
  42.         }  
  43.         if((ip1result[0]==ip2result[0])&&(ip1result[1]==ip2result[1])&&(ip1result[2]==ip2result[2])&&(ip1result[3]==ip2result[3])){  
  44.             return 0;  
  45.         }  
  46.         else{  
  47.             return 2;  
  48.         }  
  49.           
  50.     }  
  51.     public static void main(String[] args) {  
  52.         Scanner cin = new Scanner(System.in);  
  53.         Main main = new Main();  
  54.         int flags = main.checkNetSegment(cin.next(),cin.next(),cin.next());  
  55.         System.out.println(flags);  
  56.     }  
  57. }  

8 京东机试2016

采用动态规划,另外注意对于输入的数据到二维数组中,对于一个二维数组,int[][] input=new int[][],  input.length是行的长度,input[0].length是列的长度,注意

参考资料:http://www.cnblogs.com/luxiaoxun/archive/2012/11/15/2771605.html 

http://codercareer.blogspot.com/2014/10/no-56-maximal-value-of-gifts.html

[java] view plain copy
  1. import java.util.Scanner;  
  2.   
  3. public class Main{  
  4.     public static int getMaxValue(int[][] input) {  
  5.         int rows = input.length;  
  6.         int cols = input[0].length;  
  7.         int[][] maxinput = new int[rows][cols];  
  8.         for(int i = 0; i < rows; ++i) {  
  9.             for(int j = 0; j < cols; ++j) {  
  10.                 int left = 0;  
  11.                 int up = 0;  
  12.    
  13.                 if(i > 0) {  
  14.                     up = maxinput[i - 1][j];  
  15.                 }  
  16.                 if(j > 0) {  
  17.                     left = maxinput[i][j - 1];  
  18.                 }  
  19.                 maxinput[i][j] = Math.max(left, up) + input[i][j];  
  20.             }  
  21.         }  
  22.         return maxinput[rows - 1][cols - 1];  
  23.     }  
  24. /*    public static int getMaxValue(int[][] values) { 
  25.         int rows = values.length; 
  26.         int cols = values[0].length; 
  27.   
  28.         int[] maxValues = new int[cols]; 
  29.         for(int i = 0; i < rows; ++i) { 
  30.             for(int j = 0; j < cols; ++j) { 
  31.                 int left = 0; 
  32.                 int up = 0; 
  33.   
  34.                 if(i > 0) { 
  35.                     up = maxValues[j]; 
  36.                 } 
  37.   
  38.                 if(j > 0) { 
  39.                     left = maxValues[j - 1]; 
  40.                 } 
  41.   
  42.                 maxValues[j] = Math.max(left, up) + values[i][j]; 
  43.             } 
  44.         } 
  45.   
  46.         return maxValues[cols - 1]; 
  47.     }*/  
  48.     public static void main(String[] args) {  
  49.         Scanner cin = new Scanner(System.in);  
  50.         int input[][] = new int[6][6];  
  51.         for(int i=0;i<6;i++){  
  52.             for(int j=0;j<6;j++){  
  53.                 input[i][j]= cin.nextInt();  
  54.             }  
  55.         }  
  56.         int result = getMaxValue(input);  
  57.         System.out.println(result);  
  58.     }  
  59. }  

9 京东机试2016



[cpp] view plain copy
  1. //设原有苹果a1只,   
  2. //第一只猴取后剩下a2 = (4 / 5)(a1 - 1)只,a2 + 4 = (4 / 5)(a1 + 4)  
  3. //第二只猴取后剩下a3 = (4 / 5)(a2 - 1)只,a3 + 4 = (4 / 5)(a2 + 4)  
  4. //第三只猴取后剩下a4 = (4 / 5)(a3 - 1)只,a4 + 4 = (4 / 5)(a3 + 4)  
  5. //第四只猴取后剩下a5 = (4 / 5)(a4 - 1)只,a5 + 4 = (4 / 5)(a4 + 4)  
  6. //第五只猴取后剩下a6 = (4 / 5)(a5 - 1)只,a6 + 4 = (4 / 5)(a5 + 4)  
  7. //第五只猴取走的是(a5 - 1) / 5  
  8. //a6 + 4 = (a1 + 4)*(4 / 5) ^ 5  
  9. //a6 + 4, a1 + 4都是正整数,所以a1 + 4是5 ^ 5的公倍数,最小值5 ^ 5,  
  10. #include<stdlib.h>  
  11. #include<stdio.h>  
  12. int Fun(int N)  
  13. {  
  14.     int i;  
  15.     int result=1;  
  16.     if (N < 1 || N>9)  
  17.         return NULL;  
  18.     else {  
  19.         for (i = 1; i <= N; i++)  
  20.             result *= N;  
  21.         result = result -N + 1;  
  22.     }  
  23.     return result;  
  24. }  
  25. void main()  
  26. {  
  27.     int N;  
  28.     scanf("%d",&N);  
  29.     printf("%d\n", Fun(N));  
  30. }  

10 华为机试 上海 2016

9

本题目是放在了华为机试的第二题,感觉难度还是有些大的,这个题目类似于走迷宫问题,可以参考百度文库的ppt,将的很好,形象,关键问题在于:

(1)要重新构建一个二维数组,并且在原来基础上列数和行数都增加2,对增加的部分设为临界区,目的不让出界。

(2)要构建一个栈,每前进一部,入栈,每后退一步出栈。

(3)对于在某个点上可以按照,逆时针顺序,向从向下方向开始。

下面是代码部分,有注释:

[java] view plain copy
  1. package com.cisco.huawei;  
  2.   
  3. import java.util.*;    
  4. class Step {    
  5.         int x, y, d;    
  6.     
  7.         public Step(int x, int y, int d) {    
  8.                 this.x = x;    
  9.                 this.y = y;    
  10.                 this.d = d;    
  11.         }    
  12.     
  13.         public String toString() {    
  14.                 return "[" + x + ", " + y + "]";    
  15.         }    
  16. }    
  17. public class Main {    
  18.         public boolean canReachHome(char[][] board) {    
  19.                 //定义四个方向分别是上,右,下,左  
  20.                 int[][] move = { { 01 }, { 10 }, { 0, -1 }, { -10 } };   
  21.                 Stack<Step> s = new Stack<>();    
  22.                 return path(board, move, s);    
  23.         }    
  24.         private boolean path(char[][] board, int[][] move, Stack<Step> s) {    
  25.                 int m = board.length + 2;    
  26.                 int n = board[0].length + 2;    
  27.                 char[][] maze = new char[m][n];  
  28.                 //新建了一个maze对board的四周加了一层保护膜,防止出界  
  29.                 for (int i = 0; i < n; i++) {    
  30.                         maze[0][i] = '*';    
  31.                         maze[m - 1][i] = '*';    
  32.                 }    
  33.                 for (int i = 0; i < m; i++) {    
  34.                         maze[i][0] = '*';    
  35.                         maze[i][n - 1] = '*';    
  36.                 }    
  37.                 //把maze的非四周部分内容,用board的内容替换  
  38.                 for (int i = 1; i < m - 1; i++) {    
  39.                         for (int j = 1; j < n - 1; j++) {    
  40.                                 maze[i][j] = board[i - 1][j - 1];    
  41.                         }    
  42.                 }    
  43.                 int tempx = 0;    
  44.                 int tempy = 0;    
  45.                 int finalx = 0;    
  46.                 int finaly = 0;   
  47.                 //找出,B(出发点) H(结束点),并记住坐标  
  48.                 for (int i = 0; i < m; i++) {    
  49.                         for (int j = 0; j < n; j++) {    
  50.                                 if (maze[i][j] == 'B') {    
  51.                                         tempx = i;    
  52.                                         tempy = j;    
  53.                                 }    
  54.                                 if (maze[i][j] == 'H') {    
  55.                                         finalx = i;    
  56.                                         finaly = j;    
  57.                                         maze[i][j] = '-';    
  58.                                 }    
  59.                         }    
  60.                 }    
  61.                 Step temp = new Step(tempx, tempy, -1);    
  62.                 s.push(temp);  
  63.                 //每前进一步,入栈,每回退一部出栈  
  64.                 while (!s.isEmpty()) {    
  65.                         s.pop();    
  66.                         if (!s.isEmpty()) {    
  67.                                 temp = s.peek();    
  68.                         }    
  69.                         int x = temp.x;    
  70.                         int y = temp.y;    
  71.                         int d = temp.d + 1;    
  72.                         //int[][] move = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };    
  73.                         while (d < 4) {    
  74.                             //往上下左右四个方向,依次去试,如果有等于'-'的情况,则入栈      
  75.                             int i = x + move[d][0];    
  76.                                 int j = y + move[d][1];    
  77.                                 if (maze[i][j] == '-') {    
  78.                                         temp = new Step(i, j, -1);    
  79.                                         s.push(temp);    
  80.                                         x = i;    
  81.                                         y = j;    
  82.                                         maze[x][y] = '#';    
  83.                                         if (x == finalx && y == finaly) {    
  84.                                                 return true;    
  85.                                         } else {    
  86.                                                 d = 0;    
  87.                                         }    
  88.                                 } else {    
  89.                                         d++;    
  90.                                 }    
  91.                         }    
  92.                 }    
  93.                 return false;    
  94.         }    
  95.         public static void main(String args[]) {    
  96.                 Scanner cin = new Scanner(System.in);    
  97.                 int row, col;    
  98.                 Main m = new Main();    
  99.     
  100.                 while (cin.hasNext()) {    
  101.                         row = Integer.valueOf(cin.nextLine());    
  102.                         col = Integer.valueOf(cin.nextLine());    
  103.                         char[][] board = new char[row][col];    
  104.                         for (int i = 0; i < row; i++) {    
  105.                                 String str = cin.nextLine();    
  106.                                 for (int j = 0; j < col; j++) {    
  107.                                         board[i][j] = str.charAt(j);    
  108.                                 }    
  109.                         }    
  110.                         boolean ans = m.canReachHome(board);    
  111.                         if (ans) {    
  112.                                 System.out.println("Y");    
  113.                         } else {    
  114.                                 System.out.println("N");    
  115.                         }    
  116.                 }    
  117.         }    
  118. }   
  119. /*1 
  120. 5 
  121. -B-H#*/  

11 和尚挑水问题华为机试 2016

参考:http://blog.csdn.net/sunnyyoona/article/details/46778981

http://blog.csdn.net/qq_18989901/article/details/48413087

我是把第二位大神的改成了用java的代码:

[java] view plain copy
  1. package com.cisco.huawei;  
  2. import java.util.Scanner;  
  3. public class Main {   
  4.     private static int day = 8;  
  5.     private static int count=0;  
  6.     private static int spare[][] = new int[8][8];  
  7.     private static int monk[] = new int[8];  
  8.     private static int done[] = new int[8];  
  9.     public void printMonk(int t){  
  10.         if(t >= 8){  
  11.             for(int i=1;i<8;i++){  
  12.                 System.out.print(monk[i] + " ");  
  13.             }  
  14.             System.out.println("");  
  15.             count++;  
  16.         }  
  17.         else{  
  18.             for(int i=1;i<8;i++){  
  19.                 monk[t] = i;  
  20.                 if(done[i] == 0 && spare[i][t] == 1){  
  21.                     done[i]=1;  
  22.                     printMonk(t+1);  
  23.                     done[i] =0;  
  24.                 }  
  25.             }  
  26.         }  
  27.     }  
  28.     public static void main(String args[]) {    
  29.             Main main = new Main();      
  30.             Scanner cin = new Scanner(System.in);    
  31.             for(int i=1;i<8;i++){  
  32.                 for(int j=1;j<8;j++){  
  33.                     spare[i][j] = cin.nextInt();  
  34.                 }  
  35.             }  
  36.             for(int i=1;i<8;i++){  
  37.                 monk[i] = 0;  
  38.                 done[i] = 0;  
  39.             }  
  40.             main.printMonk(1);  
  41.             System.out.println("count"+count);  
  42.         }    
  43. }  
  44. /* 
  45. 0 1 0 1 0 0 0 
  46. 1 0 0 0 0 1 0 
  47. 0 0 1 0 0 0 1 
  48. 0 0 0 0 1 0 0 
  49. 1 0 0 1 0 1 0 
  50. 0 1 0 0 1 0 0 
  51. 0 0 1 0 0 1 1 
  52. */  

下面这段是先传输方案总数,然后输出每一个方案,是先用temp把方案存起来:

[java] view plain copy
  1. package com.cisco.huawei;  
  2. import java.util.Scanner;    
  3. public class Main {     
  4.    // private static int day = 8;    
  5.     private static int count=0;    
  6.     private static int spare[][] = new int[8][8];//放入输入的数据  
  7.     private static int temp[][] = new int[1000][8];  
  8.     private static int day[] = new int[8];//7天,其中第一天不算,每周几哪个和尚挑水  
  9.     private static int done[] = new int[8];//7天 记录哪个和尚挑过水了  
  10.     public void printday(int t){  //t表示周几  
  11.         if(t >= 8){    
  12.             count++;   
  13.             for(int i=1;i<8;i++){    
  14.                 temp[count][i]=day[i];  
  15.                 //System.out.print(day[i] + " ");    
  16.             }    
  17.             //System.out.println("");    
  18.              
  19.         }    
  20.         else{    
  21.             for(int i=1;i<8;i++){    
  22.                 day[t] = i;    
  23.                 if(done[i] == 0 && spare[i][t] == 1){    
  24.                     done[i]=1;    
  25.                     printday(t+1);    
  26.                     done[i] =0;    
  27.                 }    
  28.             }    
  29.         }    
  30.     }    
  31.     public static void main(String args[]) {      
  32.             Main main = new Main();        
  33.             Scanner cin = new Scanner(System.in);      
  34.             for(int i=1;i<8;i++){    
  35.                 for(int j=1;j<8;j++){    
  36.                     spare[i][j] = cin.nextInt();    
  37.                 }    
  38.             }    
  39.             main.printday(1);    
  40.             System.out.println("count"+count);   
  41.             for(int i=1;i<=count;i++){  
  42.                 for(int j=1;j<8;j++){  
  43.                     System.out.print(temp[i][j]+" ");  
  44.                 }  
  45.                 System.out.println();  
  46.             }  
  47.         }      
  48. }   

下面是c++代码实现:主要就是用:

vector<vector<int >> spare(7,vector<int>(7,0))实现二维数组

vector<int> week(7,0) 一维数组

[cpp] view plain copy
  1. #include <iostream>  
  2. #include <vector>  
  3. #include <string>  
  4. #include <stack>  
  5. #include <algorithm>  
  6. using namespace std;  
  7. void DrawingWater(vector<vector<int> > &spare,int index,vector<int> &week,vector<bool> &visited,vector<vector<int> > &t,int &num)  
  8. {  
  9.     int i;  
  10.     if(index==7)  
  11.     {  
  12.         ++num;  
  13.         for(i=0;i<7;i++)  
  14.             t[num][i]=week[i]+1;  
  15.         return;  
  16.     }  
  17.     for(i=0;i<7;++i)  
  18.     {  
  19.         week[index]=i;//week[0]=0; index是第几个和尚,  
  20.         if (!visited[i]&&spare[i][index]==1) {//这个和尚没有挑过水,并且可以挑水。就是一个  
  21.             visited[i]=1;//说明这个和尚挑过水  
  22.             DrawingWater(spare,index+1,week,visited,t,num);  
  23.             visited[i]=false;//ok  
  24.         }  
  25.     }  
  26. }  
  27. int main(){  
  28.     vector<vector<int> > spare(7,vector<int>(7,0));//第一个7行,第二个  
  29.     vector<vector<int> > t(1000,vector<int>(7,0));//先储存起来  
  30.     int num=0;  
  31.     for(int i = 0;i < 7;++i){  
  32.   
  33.         for(int j = 0;j < 7;++j){  
  34.             cin>>spare[i][j];  
  35.         }  
  36.     }  
  37.     int count = 0;  
  38.     vector<int> week(7,0);//7天,都是0,是为了记录到底是周几  
  39.     vector<bool> visited(7,false);//那个和尚是否挑过水  
  40.     DrawingWater(spare,0,week,visited,t,num);  
  41.     cout<<num<<endl;  
  42.     for(int i=1;i<=num;i++)  
  43.     {  
  44.         for(int j=0;j<7;j++)  
  45.             cout<<t[i][j]<<" ";  
  46.         cout<<endl;  
  47.     }  
  48.     return 0;  
  49. }  


12 去哪儿网笔试2016


下面这份代码测试为86%

[java] view plain copy
  1. import java.util.ArrayList;  
  2. import java.util.Collections;  
  3. import java.util.Comparator;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6. import java.util.Map.Entry;  
  7. import java.util.Scanner;  
  8. import java.util.TreeMap;  
  9.   
  10. public class Main {   
  11.     public int linuxCheck(Map<String,Integer> input){  
  12.              
  13.         List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(input.entrySet());    
  14.         Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {  
  15.   
  16.             @Override  
  17.             public int compare(Entry<String, Integer> o1,  
  18.                     Entry<String, Integer> o2) {  
  19.                 // TODO Auto-generated method stub  
  20.                 return o2.getValue().compareTo(o1.getValue());  
  21.             }  
  22.         });  
  23.         int ll=0;  
  24.         for(Map.Entry<String, Integer> mapping:list){    
  25.             String[] keys = mapping.getKey().split("\\.");  
  26.             int flags = Integer.parseInt(keys[1])%2;  
  27.             ll++;  
  28.             if(flags ==0){  
  29.                 System.out.println(mapping.getKey());  
  30.                 ll=1;  
  31.                 return 1;  
  32.             }  
  33.             if(ll==0){  
  34.                 System.out.println("no stable available");  
  35.             }  
  36.             //System.out.println(mapping.getKey()+":"+mapping.getValue());    
  37.         }  
  38.         return 0;    
  39.     }  
  40.     public static void main(String args[]) {    
  41.             Main main = new Main();      
  42.             Scanner cin = new Scanner(System.in);  
  43.             Map<String,Integer> map = new TreeMap<String, Integer>();  
  44.             int count = Integer.parseInt(cin.nextLine());  
  45.             String[] input = new String[count];  
  46.             for(int i=0;i<count;i++){  
  47.                 input[i]=cin.nextLine();  
  48.                 String[] value = input[i].split("\\.");  
  49.                 map.put(input[i], Integer.parseInt(value[0]));  
  50.             }  
  51.             main.linuxCheck(map);  
  52.         }    
  53. }  
  54. */  
  55. 7  
  56. 10.3.5  
  57. 9.2.16  
  58. 11.4.20  
  59. 11.3.14  
  60. 2.1.12  
  61. 12.4  
  62. 13.5  

13 去哪儿网笔试2016


下面这份代码测试通过100%,刚开始的时候,没有对三个数组进行排序,直接int[] array = new int[65536]是不正确的,后来对三个数组进行排序后,选出最大的值然后,int [] array = new int[max]正确

[java] view plain copy
  1. import java.util.ArrayList;  
  2. import java.util.Collections;  
  3. import java.util.Comparator;  
  4. import java.util.List;  
  5. import java.util.Scanner;  
  6.   
  7. public class Main {   
  8.     public int searchPublic(List<Integer> alist,List<Integer> blist,List<Integer> clist){  
  9.         Collections.sort(alist,new Comparator<Integer>() {  
  10.   
  11.             @Override  
  12.             public int compare(Integer o1, Integer o2) {  
  13.                 // TODO Auto-generated method stub  
  14.                 return o2 - o1;  
  15.             }  
  16.         });  
  17.         Collections.sort(blist,new Comparator<Integer>() {  
  18.   
  19.             @Override  
  20.             public int compare(Integer o1, Integer o2) {  
  21.                 // TODO Auto-generated method stub  
  22.                 return o2 - o1;  
  23.             }  
  24.         });  
  25.         Collections.sort(clist,new Comparator<Integer>() {  
  26.   
  27.             @Override  
  28.             public int compare(Integer o1, Integer o2) {  
  29.                 // TODO Auto-generated method stub  
  30.                 return o2 - o1;  
  31.             }  
  32.         });  
  33.           
  34.         int max = alist.get(0)> blist.get(0)? alist.get(0):blist.get(0);  
  35.         int maxx = max > clist.get(0)? max:clist.get(0);  
  36.         int[] array = new int[maxx+1];  
  37.   
  38.         for(int i=0;i<alist.size();i++){  
  39.             array[alist.get(i)]= array[alist.get(i)] + 1;  
  40.         }  
  41.         for(int i=0;i<blist.size();i++){  
  42.             array[blist.get(i)]= array[blist.get(i)] + 1;  
  43.         }  
  44.         for(int i=0;i<clist.size();i++){  
  45.             array[clist.get(i)]= array[clist.get(i)] + 1;  
  46.         }  
  47.         for(int i=0;i<array.length;i++){  
  48.             if(array[i] == 3){  
  49.                 System.out.print(i+" ");  
  50.             }  
  51.         }  
  52.         return 0;    
  53.     }  
  54.     public static void main(String args[]) {    
  55.             Main main = new Main();      
  56.             Scanner cin = new Scanner(System.in);  
  57.             int a = Integer.parseInt(cin.nextLine());  
  58.             List<Integer> alist = new ArrayList<Integer>(a);  
  59.             for(int i=0;i<a;i++){  
  60.                 alist.add(Integer.parseInt(cin.nextLine()));  
  61.             }  
  62.             int b = Integer.parseInt(cin.nextLine());  
  63.             List<Integer> blist = new ArrayList<Integer>(b);  
  64.             for(int i=0;i<b;i++){  
  65.                 blist.add(Integer.parseInt(cin.nextLine()));  
  66.             }  
  67.             int c = Integer.parseInt(cin.nextLine());  
  68.             List<Integer> clist = new ArrayList<Integer>(c);  
  69.             for(int i=0;i<c;i++){  
  70.                 clist.add(Integer.parseInt(cin.nextLine()));  
  71.             }  
  72.             main.searchPublic(alist,blist,clist);  
  73.         }    
  74. }  
  75. /* 
  76. 10 
  77. 1 
  78. 9 
  79. 30 
  80. 2 
  81. 7 
  82. 10 
  83. 94 
  84. 64 
  85. 82 
  86. 13 
  87. 9 
  88. 4 
  89. 9 
  90. 5 
  91. 23 
  92. 65 
  93. 34 
  94. 2 
  95. 10 
  96. 21 
  97. 8 
  98. 10 
  99. 97 
  100. 47 
  101. 38 
  102. 82 
  103. 91 
  104. 2 
  105. 71 
  106. */  

14 去哪儿笔试2016


代码,当时没有写出来,后面有时间更行ing

15 网易笔试2016

  网易笔试,这次不知道为什么公司的网站就是登不上。然后找同学做的,不管结果咋样了,就算是经历一次吧。  

   这次网易的笔试的输入输出有点坑,不像其他笔试一样告诉你输入时多上行,而是没有告诉你输入是多上行数据,这样每输入一次完整数据就会有输出,并且一直检测输入,用while(cin.hasNext)


难度不大,主要是求了一个最大公约数:

[java] view plain copy
  1. import java.util.Scanner;  
  2.   
  3. public class Main {  
  4.   
  5.     public static void main(String[] args) {  
  6.         Scanner cin = new Scanner(System.in);  
  7.         while (cin.hasNext()){  
  8.         int n = cin.nextInt();  
  9.         int a = cin.nextInt();  
  10.         int arr[] = new int[n];  
  11.         for(int i = 0;i< n;i++)  
  12.         {  
  13.             arr[i] = cin.nextInt();  
  14.         }  
  15.         int currenta = a;  
  16.         for(int i = 0;i< n;i++)  
  17.         {  
  18.             if(currenta > arr[i])  
  19.             {  
  20.                 currenta += arr[i];  
  21.             }  
  22.             else  
  23.                 currenta += gcd(arr[i],currenta);  
  24.         }  
  25.         System.out.println(currenta);  
  26.         }  
  27.           
  28.     }  
  29. //求最大公约数  
  30.     public static int gcd(int a,int b)    
  31.     {     
  32.           
  33.         if(a < b){    
  34.             return gcd(b,a);    
  35.         }    
  36.         if(b == 0){    
  37.             return a;    
  38.         }    
  39.         else{    
  40.             return gcd(a - b,b);    
  41.         }    
  42.     }  
  43.   
  44. }  

0 0
原创粉丝点击