/*
* 创建日期 2008-8-3
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package roundRobin;
import java.util.ArrayList;
import java.util.Arrays;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2007</p>
* <p>Company: ADTEC</p>
*
* @author 张也弛
* @version 1.00
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class StandardRound {
public static int mathDivisor(int[] ori){
int[] newInt = (int[])ori.clone();
Arrays.sort(newInt);
// for(int i = 0; i < ori.length; i++){
//
// System.out.println("ori["+i+"]="+ori[i]);
//
// }
int result = 1;
boolean flag = false;
for(int i = 2; i < newInt[0] + 1; i++){
flag = true;
for(int j = 0; j < newInt.length; j++){
if(newInt[j] % i != 0)
flag = false;
}
if(true == flag){
result = i;
}
}
return result;
}
public static int[] standardRound(int[] oriInt){
//可能要去掉
for( int i = 0;i < oriInt.length; i++ ){
if( oriInt[i] == 0 )
oriInt[i] = 1;
}
//可能要去掉 end
ArrayList arrayList = new ArrayList();
int commonDivisorInt = mathDivisor(oriInt);
arrayList = standardRound( oriInt, arrayList, commonDivisorInt );
int[] result = new int[0];
String[] resultStr = (String[])arrayList.toArray(new String[0]);
result = new int[resultStr.length];
for(int i = 0; i < resultStr.length; i++){
result[i] = Integer.parseInt(resultStr[i]);
}
return result;
}
public static ArrayList standardRound(int[] oriInt, ArrayList arrayList, int commonDivisorInt){
if(oriInt == null || oriInt.length == 0){
return arrayList;
}
int maxNumber = getMaxNumber(oriInt);
if( 0 >= maxNumber){
return arrayList;
}
int maxNumPos = findMaxNumber(oriInt, maxNumber);
if( -1 == maxNumPos ){
return arrayList;
}
arrayList.add( Integer.toString(maxNumPos) );
oriInt[maxNumPos] = oriInt[maxNumPos] - commonDivisorInt;
standardRound(oriInt, arrayList, commonDivisorInt);
return arrayList;
}
public static int getMaxNumber(int[] oriInt){
int[] newInt = (int[])oriInt.clone();
Arrays.sort(newInt);
return newInt[ newInt.length -1 ];
}
public static int findMaxNumber(int[] oriInt, int maxNumber){
for(int i = 0; i < oriInt.length; i++){
if( oriInt[i] == maxNumber ){
return i;
}
}
return -1;
}
public static void main(String[] args) {
int test[] = {20,5,10};
test = standardRound(test);
for(int i = 0; i < test.length; i++){
System.out.println("第"+(i+1)+"次连接第"+(test[i]+1)+"个服务器");
}
}
}