C语言 c++ php mysql nginx linux lnmp lamp lanmp memcache redis 面试 笔记 ppt 设计模式 问题 远程连接

java小练习

1.冒泡,选择排序

public class TestA {

    public static void main(String[] args) {
        int[] arr = new int[args.length];
        for(int i=0;i<args.length;i++)
        {
            arr[i] = Integer.parseInt(args[i]);
        }    

        bubbleSort(arr);
        print(arr);
        
    }
    
    //相邻之间交换
    public static void bubbleSort(int[] arr){
        int len = arr.length, tmp;
        
        for(int i=len-1;i>=1;i--)
        {
            for(int j=0;j<i-1;j++)
            {
                if(arr[j]>arr[j+1])
                {
                    tmp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = tmp;
                }    
            }
        }
    }
    
    //这一个依次和其他对比交换
    public static void selectSort(int[] arr){
        int k,tmp;
        for(int i=0;i<arr.length;i++)
        {
            k = i;
            for(int j=i+1;j<arr.length;j++)
            {
                if(arr[k]>arr[j])
                {
                    k = j;
                }    
            }    
            
            if(k != i)
            {
                tmp = arr[i];
                arr[i] = arr[k];
                arr[k] = tmp;
            }    
            
        }
    }
    
    public static void print(int[] arr)
    {
        for(int i=0;i<arr.length;i++)
        {
            System.out.print(arr[i]+" ");
        }    
    }

}

 2.二分法查找

  

public class TestSearch {
    public static void main(String[] args) {
        int a[] = { 1, 3, 6, 8, 9, 10, 12, 18, 20, 34 };
        int i = 12;
        //System.out.println(search(a, i));
        System.out.println(binarySearch(a, i));
    }
    
    public static int search(int[] a, int num) {
        for(int i=0; i<a.length; i++) {
            if(a[i] == num) return i;
        }
        return -1;
    }
    
    public static int binarySearch(int[]a, int num) {
    if (a.length==0) return -1;
    
    int startPos = 0; 
    int endPos = a.length-1;
    int m = (startPos + endPos) / 2;
    while(startPos <= endPos){
      if(num == a[m]) return m;
      if(num > a[m]) {
          startPos = m + 1;
      }
      if(num < a[m]) {
          endPos = m -1;
      }
      m = (startPos + endPos) / 2;
    }
    return -1;
  }

}

 

 3.

/**
 * 500个小孩围成一圈,每数到3则推出,继续数,计算最后那个小孩的位置
 */
public class count3Quit {

    public static void main(String[] args) {
        boolean[] arr = new boolean[500];
        for(int i=0;i<arr.length;i++)
        {
            arr[i] = true;
        }    
        //剩余小孩的数量
        int leftCount = arr.length;
        
        //数组当前元素的下标
        int index = 0;
        
        //最大值
        int countNum = 0;    
        
        while(leftCount>1)
        {
            if(arr[index] == true)
            {
                countNum ++;
                if(countNum == 3)
                {
                    countNum = 0;
                    arr[index] = false;
                    leftCount --;
                }    
            }
            
            //保证数组一直循环下去
            index ++;
            if(index == arr.length)
            {
                index = 0;
            }    
        }
        
        //找出最后元素的位置
        for(int i=0;i<arr.length;i++)
        {
            if(arr[i] == true)
            {
                System.out.println(i);
            }    
        }
        
    }
}

 4.上面的问题,用面向对象的方式实现

//500个小孩围成一圈,每数到3则推出,继续数,计算最后那个小孩的位置
public class count3Quit {
    public static void main(String[] args) {
        KidCircle kc = new KidCircle(10);
        
        kc.print();
        
        /*int countNum = 0;
        Kid k = kc.first;
        while(kc.count > 1) {
            countNum ++;
            if(countNum == 3) {
                countNum = 0;
                kc.delete(k);
            }
            k = k.right;
        }
        
        //这时只剩一个元素
        System.out.println(kc.first.id);*/
    }
}

class Kid {
    int id;
    Kid left;
    Kid right;
}

class KidCircle {
    int count = 0;
    Kid first, last;
    
    KidCircle(int n) {
        for(int i=0; i<n; i++) {
            add();
        }
    }
    
    //增加元素
    void add() {
        Kid k = new Kid();
        k.id = count;
        if(count <= 0) {
            first = k;
            last = k;
            k.left = k;
            k.right = k;
        } else {
            last.right = k;
            k.left = last;
            k.right = first;
            first.left = k;
            last = k;
        }
        count ++;
    }
    
    //删除元素
    void delete(Kid k) {
        if(count <= 0) {
            return;
        } else if (count == 1) {
            first = last = null;
        } else {
            k.left.right = k.right;
            k.right.left = k.left;
            
            if(k == first) {
                first = k.right;
            } else if( k == last) {
                last = k.left;
            }
        }
        count --;
    }
    
    //遍历圈
    void print()
    {
        Kid tmp = first;
    
/*        while(true)
        {
            System.out.println("id:"+tmp.id);
            tmp = tmp.right;
            if(tmp == first)
            {
                System.exit(-1);
            }
        }    */
        
        for(int i=0;i<count;i++)
        {
            System.out.println("id:"+tmp.id);
            tmp = tmp.right;
        }
    }
}

 

 

 

 

 

posted on 2013-09-14 15:09  思齐_  阅读(430)  评论(0编辑  收藏  举报