rt,行如:
123456789
123456798
123456879
。。
要算法,九重循环免,谢谢~~

解决方案 »

  1.   

    下面是以前写的,可能不够精简:#include <iostream.h>
    #include <string.h>
    #include <memory.h>void pailie( char *s )
    {
    int nLen = strlen(s); static char *head = new char[nLen];
    static char *headtemp = new char[nLen];
    static int first = 1;
    if( first )
    {
    memset( head, 0, nLen );
    first = 0;
    }  if( nLen == 1 )
    {
    cout << head;
    cout << s << "   ";
    return;
    } char * temp = new char[nLen];
    memset( temp, 0, nLen ); int nheadlen = strlen(head);
    strcpy( headtemp, head ); for( int n = 0; n < nLen; n++ )
    {
    if( n )
    strncpy( temp, s, n );
    if( nLen - n - 1 )
    strncpy( temp + n, s + n + 1, nLen - n - 1 );
    temp[nLen - 1] = 0; head[nheadlen] = s[n];
    head[nheadlen + 1] = 0;
    pailie( temp );
    strcpy( head, headtemp );
    } delete[] temp; if( nheadlen == 0 )
    {
    delete[] head;
    delete[] headtemp;
    }
    }int main(int argc, char* argv[])
    {
    pailie("123456789");
    cout << endl;
    return 0;
    }
      

  2.   

    template <class T>
    inline void Swap(T& a, T& b)
    {// 交换a和b
    T temp = a; a = b; b = temp;
    }template<class T>
    void Perm(T list[], int k, int m)
    { / /生成list [k:m ]的所有排列方式
    int i;
    if (k == m) {//输出一个排列方式
    for (i = 0; i <= m; i++)
    cout << list [i];
    cout << endl;
    }
    else // list[k:m ]有多个排列方式
    // 递归地产生这些排列方式
    for (i=k; i <= m; i++) {
    Swap (list[k], list[i]);
    Perm (list, k+1, m);
    Swap (list [k], list [i]);
    }
    }