|
public class mytest
{String[] mychar=new String[50];
int charcount;
int charlist;
public void SetMyTest()
//初始化
{charcount=0;
charlist=1;
}
public void insertChar(String thischar)
//增加新的字符串
{charcount++;
mychar[charcount]=thischar;
charlist*=charcount;
}
public String listAllChar()
//列出全排列
{String[][] allchar=new String[charlist+1][charcount+1];
int i;
int j;
int z=1;
for (i=1;i<=charcount;i++)
{myCopy(addCharList(i,allchar,z),allchar,charlist,charcount);
z*=i;
}
String listallchar=new String("");
for (i=1;i<=charlist;i++)
{for (j=1;j<=charcount;j++)
listallchar+=allchar[j]+" ";
listallchar=listallchar+"
";
}
return listallchar;
}
public String[][] addCharList(int i,String[][] allchar,int z)
//在i-1个对象的全排列中引入第i个对象
{int j;
int h=1;
int k;
String[][] tempallchar=new String[charlist+1][charcount+1];
for (k=1;k<=z;k++)
{for (j=1;j<=i;j++)
{myCopy(tempchar(j,allchar[k],mychar),tempallchar[h],charcount);
h++;
}
}
return tempallchar;
}
public String[] tempchar(int i,String[] beginchar,String thischar)
//将新对象插入指定位置
{int j;
String[] tempbeginchar=new String[charcount+1];
myCopy(beginchar,tempbeginchar,charcount);
for (j=charcount;j>i;j--) tempbeginchar[j]=tempbeginchar[j-1];
tempbeginchar=thischar;
return tempbeginchar;
}
public String selectSomeChar(int select)
//列出其中取出select个对象的全部组合
{int selectcount=1;
int i;
for (i=select+1;i<=charcount;i++) selectcount=selectcount*i/(i-select);
String[][] selectchar=new String[selectcount+1][select+1];
int[][] selectint=new int[selectcount+1][select+1];
for (i=1;i<=select;i++)
{selectchar[1]=mychar;
selectint[1]=i;
}
int z=1;
addSelect(selectchar,selectint,1,select,select);
int j;
String selectsomechar=new String("");
for (i=1;i<=selectcount;i++)
{for (j=1;j<=select;j++)
selectsomechar+=selectchar[j]+" ";
selectsomechar=selectsomechar+"
";
}
return selectsomechar;
}
public void addSelect(String[][] selectchar,int[][] selectint,int z,int position,int select)
//增加新的组合
{int i;
if (position==select)
{if (selectint[z][position] {z++;
myCopy(selectint[z-1],selectint[z],select);
selectint[z][select]++;
for (i=1;i<=select;i++) selectchar[z]=mychar[selectint[z]];
addSelect(selectchar,selectint,z,position,select);
}
else
{position--;
addSelect(selectchar,selectint,z,position,select);
}
}
else
{if (selectint[z][position] {selectint[z][position]++;
selectint[z][position+1]=selectint[z][position]+1;
position++;
addSelect(selectchar,selectint,z,position,select);
}
else
{if (position==1)
{return;
}
else
{position--;
addSelect(selectchar,selectint,z,position,select);
}
}
}
}
public void myCopy(String[][] Str1,String[][] Str2,int i,int j)
{int h;
int k;
for (h=1;h<=i;h++) for (k=1;k<=j;k++) Str2[h][k]=Str1[h][k];
}
public void myCopy(String[] Str1,String[] Str2,int i)
{int h;
for (h=1;h<=i;h++) Str2[h]=Str1[h];
}
public void myCopy(int[] Str1,int[] Str2,int i)
{int h;
for (h=1;h<=i;h++) Str2[h]=Str1[h];
}
} |
|