黑马程序员——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();
}
}
- 黑马程序员——day04
- 黑马程序员——day04排序,数组
- 黑马程序员_java基础day04
- 黑马程序员-Java基础知识-day04
- 黑马程序员--Java基础Day04
- 黑马程序员-day04-Java基础语法(数组)
- 黑马程序员 java学习笔记(day04)
- 黑马程序员-day04数组排序,查找
- 黑马程序员--面向对象(day04)
- 黑马程序员_Java基础_我的day04笔记
- 黑马程序员 之 Java基础 IO 流中的缓冲区 机制 Day04
- 黑马程序员—泛型
- 黑马程序员—多线程
- 黑马程序员—DOM
- 黑马程序员—javabean
- 黑马程序员—xml
- 黑马程序员—多线程
- 黑马程序员—反射
- 数据流图的绘制要点
- 秒杀多线程第十一篇 读者写者问题
- Linux下java web服务器搭建(Apache2.2+svn1.6)
- XMLHttpRequest的一个特别安全问题:
- 【转】如何看待别人写的“心得,总结”
- 黑马程序员——day04
- 不容易系列之(3)—— LELE的RPG难题(递推)
- 五人过河问题的分析
- 99乘法表
- 设计模式之---Strategy 模式
- spring+hibernate
- As3 的组件资料2(不定时收集)
- 跳台阶问题
- 参照jquery_pagination写的一个生成html的类