黑马程序员——day04

来源:互联网 发布:日本搞笑电影知乎 编辑:程序博客网 时间:2024/04/30 16:47


1,折半查找

class ArrayTest
{


   public static void main(String[] arrgs)
   {
           int[] arr = {1,2,3,4,5,6,7,8,9};
           int index = halfSearch_2(arr,7);
           System.out.println("index="+index);
   } 


/*折半查找*/  
   
   public static int halfSearch_2(int[] arr,int key)
   {
      int min = 0,max = arr.length - 1,mid;
      while(min<=max)
      {
        mid = (min+max)>>1;
        if(key>arr[mid])
           min = mid + 1;
        else if(key<arr[mid])
           max = min -1;
        else 
           return mid;   
      }
      return -1;
   }


   public static int halfSearch(int[] arr, int key)
   {
      int min,max,mid;
      min = 0;   
      max = arr.length - 1;
      mid = (min+max)/2;


      while(arr[mid]!=key)
      {
        if(key>arr[mid])
          min = mid + 1;


        if(key<arr[mid])
          max = mid - 1;
        if(min>max)
          return -1;   
     
        mid = (max+min)/2;  
      } 
     return mid; 
   }
}

******************************************************************************************************************************************************************

2,进制转换

class ArrayTest2
{


   public static void main(String[] args)
   {
     //toBin(2);
     toHex(60);
   }


/*
十进制-->十六进制
*/
   public static void toHex(int num)
   {
     StringBuffer sb = new StringBuffer();
    
     for(int x=0; x<8;x++)
     {
        int temp = num & 15;
        if(temp>9)
          sb.append((char)(temp-10+'A'));
        else
          sb.append(temp);


        num = num >>> 4;   
     }


     System.out.println(sb.reverse());
   }


/*
十进制-->二进制
*/
   public static void toBin(int num)
   {
    StringBuffer sb = new StringBuffer();
    while(num>0)
    {
       sb.append(num%2);
       num = num>>>1;
    }
    System.out.println(sb.reverse()); 
   }
}

******************************************************************************************************************************************************************

class ArrayTest3
{
   //查表法
   public static void main(String[] args)
   {
      //toHex(-60);
      toBin(16);
   }


   public static void toBin(int num)
   {
      char[] chs ={'0','1'};
   
      char[] arr = new char[32];
      int pos = arr.length;


      while(num!=0)
      {
         int temp = num & 1;
         arr[--pos] = chs[temp];
         num = num >>> 1;
       } 


      for(int x = pos;x<arr.length;x++)
      {
        System.out.print(arr[x]);
       }
      System.out.println();
   }


   public static void toHex(int num)
   {
    
      char[] chs ={
                    '0','1','2','3'
                   ,'4','5','6','7'
                   ,'8','9','A','B'
                   ,'C','D','E','F' 
                   };
      char[] arr = new char[8];
      int pos = arr.length;
      while(num!=0)
      {
        int temp = num & 15;


         arr[--pos] = chs[temp];
         num = num >>> 4;
       }


       for(int x=pos;x<arr.length;x++)
       {
          System.out.print(arr[x] + ",");
        }
       System.out.println(); 
   }
}

******************************************************************************************************************************************************************

class ArrayTest4
{
   public static void main(String[] args)
   {
      
       toBin(6);
       toHex(60);
       toBa(15);
   }




  //10-->2
   public static void toBin(int num)
   {
       trans(num,1,1);
   }
  //10-->8
   public static void toBa(int num)
   {
       trans(num,7,3);
   }
  //10-->16
    public static void toHex(int num)
   {
       trans(num,15,4);
   }
   
   public static void trans(int num,int base,int offset)
   {
      if(num ==0)
      {
        System.out.println(0);
        return;
      }  
      char[] chs = {'0','1','2','3'
                  ,'4','5','6','7'
                  ,'8','9','A','B'
                  ,'C','D','E','F'};
      char[] arr = new char[32];
      int pos = arr.length;


      while(num!=0)
      {
         int temp = num & base;
         arr[--pos] = chs[temp];
         num = num >>> offset; 
       } 


      for(int x=pos;x<arr.length;x++)
      {
          System.out.print(arr[x]);
       }  
        System.out.println();
    }
}

原创粉丝点击