查表法

查表法是将一些事先计算好的结果,存储在常量数组中,运行时节省计算开销。

例如,

计算字节中位1的个数,

int countBits( unsigned char dat )

{

   static char nBitTab[256] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, ...... };

   return nBitTab[dat];

}

或将字节按位逆序,

unsigned char reverse( unsigned char dat )

{

   static char revTab[256] = { 0x0, 0x80, 0x40, ......};

   return nBitTab[dat];

}

或计算0~360度的正弦值,

int sin( int ang )
{
 static const int sinV[] = {
   /*sin00=*/0, 1144,2287,3430,4572,5712,6850,7987,9121,10252,
   /*sin10=*/11380,12505,13626,14742,15855,16962,18064,19161,20252,21336,
   /*sin20=*/22415,23486,24550,25607,26656,27697,28729,29753,30767,31772,
   /*sin30=*/32768,33754,34729,35693,36647,37590,38521,39441,40348,41243,
   /*sin40=*/42126,42995,43852,44695,45525,46341,47143,47930,48703,49461,
   /*sin50=*/50203,50931,51643,52339,53020,53684,54332,54963,55578,56175,
   /*sin60=*/56756,57319,57865,58393,58903,59396,59870,60326,60764,61183,
   /*sin70=*/61584,61966,62328,62672,62997,63303,63589,63856,64104,64332,
   /*sin80=*/64540,64729,64898,65048,65177,65287,65376,65446,65496,65526,
   /*sin90=*/65536,  
   };
 if ( 0 <= ang && ang <= 90 )
 {
  return sinV[ang];
 }
 if ( 90 < ang && ang <= 180 )
 {
  return sinV[180-ang];
 }
 if ( 180 < ang && ang <= 270 )
 {
  return -sinV[ang-180];
 }
 if ( 270 < ang && ang < 360 )
 {
  return -sinV[360-ang];
 }
 return 0;
}

理论上,y=f(x),x的取值范围为有限种状态,且f(x)有一定运算量时,都可以考虑查表法。
如,前两个例子中x的范围是字节的256个值,第三个例子是0~90度。


/** 
* 数组的操作————查表法 
* eg : 数组查表法(根据键盘录入索引,查找对应星期) 
*/  
import java.util.Scanner;  
public class arraynum {  
public static void main(String[] args) {  
Scanner sc = new Scanner(System.in);  
System.out.println("请输入对应的星期范围在1-7");  
  
int week = sc.nextInt();  
//sc.close();  
System.out.println("星期" + getWeek(week));  
}  
  
  
/* 
根据索引返回对应的星期 
1,返回值类型char 
2,参数列表int week 
*/  
//数组第0个元素为null,这样1就对应的一,以此类推  
public static char getWeek(int week) {  
char[] arr = {' ','一','二','三','四','五','六','日'};//定义了一张星期表  
return arr[week];//通过索引获取表中的元素  
}  
}  

另外,查表法还用于简化switch/case语句,还可将数据与代码分离。

推荐阅读更多精彩内容