package algorithm;
import java.util.ArrayList;
/**
* @author jupiterpan 2009-12-16
*
*/
public class PlayRoundII {
private ArrayList<Integer>[] array ;
private int count;
private int playerNo;
public PlayRoundII(){
}
public PlayRoundII(int n){
setPlayerNo(n);
count = 0;
}
@SuppressWarnings("unchecked")
private void initTeam(){
array = new ArrayList[playerNo/2];
for(int i=0;i<(playerNo/2);i++){
array[i] = new ArrayList<Integer>();
}
array[0].add(1);
array[playerNo/4].add(2);
array[playerNo/8].add(3);
array[playerNo/4+playerNo/8].add(4);
}
private void addPlayer(int n){
for(int i=0; i<playerNo/2; i++){
if(array[i].size()!=2){
array[i].add(n);
if(n==playerNo){
for(ArrayList<Integer> al:array){
System.out.print(al+" ");
}
System.out.println(++count);
}
else{
addPlayer(n+1);
}
array[i].remove(array[i].size()-1);
}
}
}
public void setPlayerNo(int n){
int m = n;
if (m<8){
System.out.println("set player's number correctly:(2^n,n>=3)");
return;
}
while((m>1)&&(m==(m>>1)<<1))m=m>>1;
if (m!=1){
System.out.println("set player's number correctly:(2^n,n>=3)");
return;
}
playerNo = n;
initTeam();
}
public void show(){
if(playerNo!=0){
addPlayer(5);
}
}
public static void main(String[] args){
new PlayRoundII(8).show();
}
}
说真的,这么写很低效,16个人我的机子跑了1个半小时才结束,原题的32人不知要多久。。。
谁有好的方法请告之啊!