`
csyjava
  • 浏览: 12798 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
社区版块
存档分类
最新评论

Java面试之旅——java算法篇之面试题目

阅读更多

 

 

求最大公约数和最小公倍数

  1. /** 
  2.  * 求最大公约数和最小公倍数 
  3.  */  
  4. public class Convention {  
  5.     /** 
  6.      * 求两数的最大公约数 
  7.      */  
  8.     int divisor(int m,int n){   
  9.         if(m%n==0){  
  10.            return n;  
  11.        }else{  
  12.            return divisor(n,m%n);  
  13.        }  
  14.     }  
  15.     /** 
  16.      * 求两数的最小公倍数 
  17.      */  
  18.     int gbs(int a,int b){  
  19.         int gbs = 0;  
  20.         gbs = a*b/divisor(a,b);  
  21.         return gbs;  
  22.     }  
  23. }  

 

求一组数组的众数

  1. public static double mode(double[] array) {
  2.  Arrays.sort(array);
  3. int count = 1;
  4. int longest = 0;
  5. double mode = 0;
  6. for (int i = 0; i < array.length - 1; i++) {
  7. if (array[i] == array[i + 1]) {
  8. count++;
  9. } else {
  10. count = 1;//如果不等于,就换到了下一个数,那么计算下一个数的次数时,count的值应该重新符值为一
  11. continue;
  12. }
  13. if (count > longest) {
  14. mode = array[i];
  15. longest = count;
  16. }
  17. }
  18. System.out.println(longest);//打印出这个数出现的次数已判断是否正确
  19. return mode;

    公司笔试题就1个,要求在10分钟内作完。

    题目如下:用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连。

 

  1. package test;
    /**
     * 用1,2,2,3,4,5,这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234,412345等,要求:“4”不能排第3位,“1”与“5”不能相连。
     * 
    @author kafka0102
     *
     
    */

    public class ATest {

        
    private static boolean is1Or5(char a,char b){
            
    if(a=='1' && b=='5')return true;
            
    if(a=='5' && b=='1')return true;
            
    return false;
        }

        
        
    private static int countOf2(char[] num,int index){
            
    int n=0;
            
    for(int i=0;i<index;i++){
                
    if(num[i]=='2')n++;
            }

            
    return n;
        }

        
        
    private static boolean hasSameNumber(int index,char a, char[] num){
            
    for(int i=0;i<index;i++){
                
    if(num[i]==a)return true;
            }

            
    return false;
        }

        
        
    public static int testArrange(char[] number,char[] num,int index){
            
    int size = 0;//组合的个数
            int pos1=1,pos2=2;//该变量为重复的2的数组位置,可以提取出来作为参数
            int emp = countOf2(num,index);//得到当前的数组中有几个2
            for(int i=0;i<num.length;i++){
                
    if(number[i]=='2'){//当前的数字为2
                    if(emp >= 2){//数组中2的个数多于2个
                        continue;
                    }
    else if(emp == 1){//数组中有一个2时,要求当前的2不能为位置1的2
                        if(i==pos1)continue;
                    }
    else{
                        
    if(i==pos2)continue;//数组中没有2时,要求当前的2不能为位置2的2
                    }

                }
    else{
                    
    if(index==2 && number[i]=='4')continue;//去除4
                    if(index>0 && is1Or5(num[index-1],number[i]))continue;//去除相邻的1和5
                    if(hasSameNumber(index,number[i], num))continue;//去除重复数字
                }

                num[index] 
    = number[i];
                
    if(index==5){
                    System.out.println(num);
                    size
    ++;
                }
    else{
                    size 
    = size + testArrange(number,num,index+1);
                }

            }

            
    return size;
        }

        
        
    public static void aTest(){
            
    char[] number = {'1','2','2','3','4','5'};
            
    char[] num = new char[number.length];
            
    int size =0;
            size 
    = testArrange(number,num,0);
            System.out.println(
    "size="+size);
        }

        
        
    public static void main(String[] args) {
            aTest();
        }


    }

 

 

 

有一个String s="SDsBEaA"
要求产生这样的结果:s="AaBDESs"

 

 

public class MySort {

        public static void main(String[] args) {
                List<Character> result = new ArrayList<Character>();
                String s = "SDsBEAa";

                char[] strArray = s.toCharArray();
                // strArray = ABDESas
                Arrays.sort(strArray);
                for (int i = 97; i < 122; i++) {
                        char tempLowCase = (char) i;
                        char tempUpperCase = (char) (i - 32);
                        for (int j = 0; j < strArray.length; j++) {
                                if (Character.isUpperCase(strArray[j])) {
                                        if (tempUpperCase == strArray[j]) {
                                                result.add(tempUpperCase);
                                        }
                                }

                                if (tempLowCase == strArray[j]) {
                                        result.add(strArray[j]);
                                }
                        }
                }

                System.out.println(result);

        }
}

 





 

 
 
 
 

本文部分代码摘自:

http://ravi.iteye.com/blog/122255(最大公约数)

http://www.blogjava.net/kafka0102/archive/2007/03/13/103434.html(数字组合)

分享到:
评论
1 楼 dingming36 2008-11-27  
不错的文章  http://www.zangao.org 中国藏獒网

相关推荐

Global site tag (gtag.js) - Google Analytics