如何按不同顺序  共N!次 来遍历数组N?谢谢如 {1,2,3} 用什么算法 可以按照以下3! = 6种顺序来遍历啊,N=100呢
123
132
213
231
321
312

解决方案 »

  1.   


    import java.io.*;class AnagramApp
       {
       static int size;
       static int count;
       static char[] arrChar = new char[100];
       //-----------------------------------------------------------
       public static void main(String[] args) throws IOException
          {
          System.out.print("Enter a word: ");    // get word
          String input = getString();
          size = input.length();                 // find its size
          count = 0;
          for(int j=0; j<size; j++)              // put it in array
             arrChar[j] = input.charAt(j);
          doAnagram(size);                       // anagram it
          }  // end main()
       //-----------------------------------------------------------
       public static void doAnagram(int newSize)
          {
          int limit;
          if(newSize == 1)                     // if too small,
             return;                           // go no further      for(int j=0; j<newSize; j++)         // for each position,
             {
             doAnagram(newSize-1);             // anagram remaining
             if(newSize==2)                    // if innermost,
                displayWord();                 // display it
             rotate(newSize);                  // rotate word
             }
          }
       //-----------------------------------------------------------
       // rotate left all chars from position to end
       public static void rotate(int newSize)
          {
          int j;
          int position = size - newSize;
          char temp = arrChar[position];       // save first letter
          for(j=position+1; j<size; j++)       // shift others left
             arrChar[j-1] = arrChar[j];
          arrChar[j-1] = temp;                 // put first on right
          }
       //-----------------------------------------------------------
       public static void displayWord()
          {
          if(count < 99)
             System.out.print(" ");
          if(count < 9)
             System.out.print(" ");
          System.out.print(++count + " ");
          for(int j=0; j<size; j++)
             System.out.print( arrChar[j] );
          System.out.print("   ");
          System.out.flush();
          if(count%6 == 0)
             System.out.println("");
          }
       //-----------------------------------------------------------
       public static String getString() throws IOException
          {
          InputStreamReader isr = new InputStreamReader(System.in);
          BufferedReader br = new BufferedReader(isr);
          String s = br.readLine();
          return s;
          }
       //-----------------------------------------------------------
       }  // end class AnagramApp
      

  2.   

    http://www.pconline.com.cn/pcedu/carton/games/0506/650563.html   就这个flash iq游戏,谁能编程算出所有可能
      

  3.   

    回复人: joyco(流星) ( ) 信誉:100  2005-07-28 19:42:00  得分: 0  
     
     
       楼上死循环了啊
      
     
    -------------------------------哪里?
      

  4.   

    如何按不同顺序  共N!次 来遍历数组N?谢谢如 {1,2,3} 用什么算法 可以按照以下3! = 6种顺序来遍历啊,N=100呢
    123
    132
    213
    231
    321
    312
    ------------------------------------LZ 那代码不正是你要的?
      

  5.   

    使用Stack,不同的进栈出栈顺序就是全排列了。。
      

  6.   

    import java.util.ArrayList;
    import java.util.Arrays;public class Permutation {
    static ArrayList resultList = new ArrayList();

    public static void getPermutation(ArrayList al, ArrayList result) {
    for(int i = 0; i < al.size(); i++) {
        Object str = al.remove(i);
        result.add(str);
        
        if(al.size() != 0)
         getPermutation(al, result);
        else {
         resultList.add((ArrayList) result.clone());
        }
        
    //     System.out.println("i= " + i + ", rst= " + result.size() + ", al= " + al.size());
        result.remove(result.size() - 1);
        al.add(i, str);
        }
    }

    public static ArrayList getPermutation(String str) {
        ArrayList al = new ArrayList();
        for(int i = 0; i < str.length(); i++){
         al.add(str.substring(i, i+1));
        }
        ArrayList result = new ArrayList();
        getPermutation(al, result);
        return resultList;
    }    public static void main(String[] args) throws Exception {
        String str = "123";
        ArrayList p = getPermutation(str);
        for(int i = 0; i < p.size(); i++){
         ArrayList p2 = (ArrayList) p.get(i);
        System.out.println(p2);
        }
        }
    }
      

  7.   

    发一用递归的算法
    public void arrange(int[] b){
    int[] a;
    int index=0;
    int j=0,k=0;int temp;
    for(int i=0;i<N;i++){
    if(b[i]==0) break;
    index++;
    }
    a=new int[index];
    for(int i=0;i<index;i++)
    a[i]=b[index-1-i];
    do{
    for(int i=0;i<a.length;i++)//输出排列中的数字
    System.out.print(a[i]);

    System.out.print("  ");

    for(j=a.length-1;j>0;j--){ //从右向左;找要交换的位置1(j);
     
     if(a[j]>a[j-1]) break;

     }
    if(j==0)break;
    j--;
    for(k=a.length-1;k>j;k--) {  //在位置1右边;从右向左;
                   //找要交换的位置2(k);
    if(a[k]>a[j])break;
    }
    temp=a[j];a[j]=a[k];a[k]=temp;
    // 交换位置1和位置2的值;
         
          for(int x=j+1,y=a.length-1;x<y;x++,y--){
           //把位置1后边的所有位反序排列;
           temp=a[x];a[x]=a[y];a[y]=temp;
           }
    }while(true);
    System.out.println();
    }
      

  8.   

    图论-------建议看看数据结构的图的部分(DFS 深度优先搜索,这个需要在回退时候改变状态)
    如果透彻,这个就肯定会了.
      

  9.   

    //附上我的算法,虽然不太好
    package TestDFS;public class TestArrangement
    {
    private int size ;
    private boolean visited[];
    private int array[];
    private int tempArray[];
    private int count; public TestArrangement()
    {
    if(size <= 0) size = 3;
    visited = new boolean[size];
    for(int i =0;i<size;i++)
    {
    visited[i] = false;
    }
    } private void  DFS(int k)
    {
    //(1)访问第K个元素,并且记录到tempArray中
    visited[k] = true;
    this.tempArray[count] = array[k];
    count ++;
    if (count == size)
    {//已经到头了,可以返回了
    this.printAllElements();
    return;
    }
    for (int j = 0; j < size; j++)
    {
    if (!visited[j] && j!=k )
    {
    //(2)如果没有访问到,则递归访问
    DFS(j); //(3)到这里表明第j个元素已经访问了,现在开始回退
    //标记没有被访问,
    visited[j] = false;
    count--;
    }
    }
    } public void  OutputArrangement()
    {
    for(int i=0;i<size;i++)
    {
    reset();
    DFS(i);
    }
    }
    public static void main(String args[])
    {
     TestArrangement t = new TestArrangement();
     int array[] = new int[]{1,2,3};
     t.setArray(array);
     t.setSize(array.length);
     t.OutputArrangement();
    }
    private void printAllElements()
    {
    for(int i =0;i<size;i++)
    {
    System.out.print(tempArray[i]);
    System.out.print(" ");
    }
    System.out.println("\n");
    }
    public void setSize(int size)
    {
    this.size = size;
    }
    public int getSize()
    {
    return size;
    }
    public int[] getArray()
    {
    return array;
    } private void reset()
    {
    for (int i = 0; i < size; i++)
    {
    this.visited[i] = false;
    this.tempArray[i] = 0;
    }
    count = 0;
    } public void setArray(int[] array)
    {
    this.array = array;
    tempArray = new int[array.length];
    }
    }
      

  10.   

    public class TestArrange {
        
        private int[] result = null;
        private int number;
        
        public TestArrange(int number) {
            this.number = number;
            result = new int[number];
        }
        
        public void arrange(int step) {
            
            if(step == number) {
                printresult();
            }
            else {
                for(int point = 1; point <= number; point ++) {
                    if( hasarranged(point, step) == false) {
                        result[step] = point;
                        arrange(step +1);
                    }
                }
            }
            
        }
        
        private void printresult() {
            for(int i =0; i<number; i++) {
                System.err.print(result[i] + " ");
            }
            System.out.println();
        }
        
        private boolean hasarranged(int point, int step) {
            boolean res = false;
            for(int i=0; i<step; i++) {
                if( result[i] == point) {
                    res = true;
                    break;
                }
            }
            return res;
        }
        
        public static void main(String[] args) {
            TestArrange ta = new TestArrange(3);
            ta.arrange(0);
        }
    }