Java算法面试题

来源:互联网 发布:写c语言是用记事本么 编辑:程序博客网 时间:2024/05/22 16:58

转载自:http://blog.csdn.net/redarmy_chen/article/details/6513541

1.比较两个字符串如果不等返回True?  

[java] view plain copy
  1. package com.test.kaoshi;  
  2.   
  3. public class StringDemo {  
  4.   
  5.     private static String a = "abc";  
  6.     private static String b = "abcg";  
  7.   
  8.     public static boolean equalString() {  
  9.         if (a.equals(b)) {  
  10.             return false;  
  11.         } else {                
  12.             return true;  
  13.         }  
  14.     }  
  15.     public static void main(String[] args) {  
  16.         StringDemo  sd = new StringDemo();  
  17.         System.out.println("主要考察返回Boolean变量和字符串比较使用的方法?"+sd.equalString());  
  18.     }  
  19. }  

2.随机产生20个字符并且排序? 

[java] view plain copy
  1. package com.test.kaoshi;  
  2.   
  3. import java.util.HashSet;  
  4. import java.util.Iterator;  
  5. import java.util.Random;  
  6. import java.util.Set;  
  7. import java.util.TreeSet;  
  8.   
  9. public class RadomDemo {  
  10. /** 
  11.  * 随机产生20个字符串并且字符串不能重复 且进行排序 
  12.  * @param random 
  13.  * @param len 
  14.  * @return 
  15.  */  
  16.     public Set getChar(){  
  17.           
  18.         Set numberSet01 = new HashSet();  
  19.         Random rdm = new Random();  
  20.         char ch;  
  21.         while(numberSet01.size()<20){   
  22.             int rdGet = Math.abs(rdm.nextInt())%26+97;//产生97到122的随机数a-z值  
  23.             ch=(char)rdGet;  
  24.             numberSet01.add(ch);  
  25.             //Set中是不能放进重复的值的,当它有20个时,就满足你的条件了   
  26.         }   
  27.         return numberSet01;  
  28.     }  
  29.     public static void main(String[] args) {  
  30.         RadomDemo rd = new RadomDemo();  
  31.         Set numberSet01=rd.getChar();  
  32.           
  33.         Set numberSet = new TreeSet();   
  34.         numberSet.addAll(numberSet01);  
  35.         for(Iterator it=numberSet01.iterator();it.hasNext();){   
  36.             System.out.print(it.next());   
  37.         }   
  38.         System.out.println();  
  39.         for(Iterator it=numberSet.iterator();it.hasNext();){   
  40.             System.out.print(it.next());   
  41.         }   
  42.     }  
  43. }  

3.50个人围成一圈数到三和三的倍数时出圈,问剩下的人是谁?在原来的位置是多少?  

Java代码 复制代码
  1. package com.test.kaoshi;   
  2.   
  3. import java.util.Iterator;   
  4. import java.util.LinkedList;   
  5.   
  6. public class YouXi {   
  7.     public static int removeNM(int n, int m) {   
  8.         LinkedList ll = new LinkedList();   
  9.         for (int i = 0; i < n; i++){   
  10.             ll.add(new Integer(i + 1));
  11.         }   
  12.        int removed = -1;   
  13.        while (ll.size() > 1) {   
  14.             removed = (removed + m) % ll.size();   
  15.             ll.remove(removed--);   
  16.         }   
  17.        return ((Integer) ll.get(0)).intValue();   
  18.     }   
  19.   
  20.     public static void main(String[] args) {   
  21.         System.out.println(removeNM(503));   
  22.     }   
  23. }  

0 0