这题有难度,用到递归算法,- class TurnStr
- {
- public static String[] mid(String[] str,String[] middle) //-----中间方法
- {
- String[] end =new String[(middle.length*(str.length-middle[0].length()))];
- int pp=0;
-
- for (int i=0;i<middle.length;i++)
- {
- for (int ii=0;ii<str.length;ii++)
- {
- if (middle[i].indexOf(str[ii])<0)
- {
- end[pp++]=middle[i]+str[ii];
- }
- }
- }
- return end;
- }
-
- public static String[] turn(String[] str) //-------------主方法
- {
- String[] end=new String[count(str.length)];
- int pp=0;
- System.arraycopy(str,0,end,pp,str.length);
- pp+=str.length;
- String[] middle =str;
- for (int i=0;i<str.length-1;i++)
- {
- middle=mid(str,middle);
- System.arraycopy(middle,0,end,pp,middle.length);
- pp+=middle.length;
- }
- return end;
- }
-
- public static int count(int length) //---------------计数返回数组维数
- {
- if (length>1)
- return length+length*count(length-1);
- else
- return 1;
- }
-
-
- public static void show(String[] str) //------------打印数组
- {
- for (String i : str)
- {
- System.out.print(i+"\t");
- }
- System.out.println();
- }
-
- public static void main(String[] args)
- {
- //String[] str={"A","B","C"};
- String[] str1={"A","B","C","D"};
- //TurnStr.show(TurnStr.turn(str));
- System.out.println(TurnStr.count(str1.length)+"--------------------------------------");
- show(turn(str1));
- }
- }
复制代码 |