首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

常见算法一

2013-12-04 
常见算法1/** * Returns the index of the min of the three indexed chars. * 获取数组三个下标中,最小值

常见算法1
/**
* Returns the index of the min of the three indexed chars.
* 获取数组三个下标中,最小值的下标 分支最多三个,最少两个。
*/
private static int min(char x[], int a, int b, int c) {

int i= 0 ;
if( x[a] < x[b] ){
if( x[b]<x[c] ){
i=a;
}else{
if( x[a]<x[c] ){
i=a;
}else{
i=c;
}
}
}else{
if( x[b]>x[c] ){
i=c;
}else{
i=b;
}
}
i = (x[a] < x[b] ? (x[b] < x[c] ? a : x[a] < x[c] ? a : c) : (x[b] > x[c] ? c : b));//等同于上面的方法
return i ;
}

/**
* Returns the index of the median of the three indexed chars.
* 获取数组三个下标中,最小值的下标 分支最多三个,最少两个。
*/
private static int med(char x[], int a, int b, int c) {

int i= 0 ;
if( x[a] < x[b] ){
if( x[b]<x[c] ){
i=b;
}else{
if(x[a]<x[c]){
i=c;
}else{
i=a;
}
}
}else{
if( x[b]>x[c] ){
i=b;
}else{
if( x[a]>x[c] ){
i=c;
}else{
i=b;
}
}
}
i = (x[a] < x[b]? (x[b] < x[c] ? b : x[a] < x[c] ? c : a) : (x[b] > x[c] ? b : x[a] > x[c] ? c : a));//等同于上面的方法
return i ;
}

/**
* Returns the index of the max of the three indexed chars.
* 获取数组三个下标中,最大值的下标 分支最多三个,最少两个。
*/
private static int max(char x[], int a, int b, int c) {

int i= 0 ;
if( x[a] > x[b] ){
if( x[b] > x[c] ){
i=a ;
}else{
if(x[a]>x[c]){
i=a;
}else{
i=c;
}
}
}else{
if( x[b]>x[c] ){
i=b;
}else{
i=c;
}
}
i = (x[a] > x[b] ? (x[b] > x[c] ? a : x[a] > x[c] ? a : c) : ( x[b] > x[c] ? b : c));//等同于上面的方法
return i ;
}

热点排行