码迷,mamicode.com
首页 > 编程语言 > 详细

全排序与康拓展开

时间:2016-12-25 14:06:51      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:n+1   swa   output   memory   排列   [1]   bsp   个数   高精度   

 

全排列:

n=3

123

132

213

231

312

321

(由1~n组成,且出现一次,从小到大排序(或从大到小排序,或不排序))

 

求全排序:

I.

Dfs1

//全排序没有顺序

#include <iostream>

using namespace std;

 

long a[100],n;

 

void swap(long &a,long &b)

{

    long temp;

    temp=a;

    a=b;

    b=temp;

}

 

void dfs(long pos)

{

    long i;

    if (pos==n)

    {

        for (i=1;i<=n;i++)

            cout<<a[i]<<" ";

        cout<<endl;

    }

    else

    {

        dfs(pos+1);

        for (i=pos+1;i<=n;i++)

        {

            swap(a[pos],a[i]);  //以a[i]作为第pos位

            dfs(pos+1);

            swap(a[pos],a[i]);

        }

    }

}

 

int main()

{

    long i;

    cin>>n;

    for (i=1;i<=n;i++)

        a[i]=i;

    dfs(1);

    return 0;

}

 

II.

Dfs2

#include <iostream>

using namespace std;

 

long n,a[1000];

/*long b[1000],ans,t;*/

bool vis[1000];

 

void dfs(long pos)

{

    long i;

    if (pos==n+1)

    {

        for (i=1;i<=n;i++)

            cout<<a[i]<<" ";

        cout<<endl;

        /*

        ans++;

        b[ans]=0;

        t=1;

        for (i=n;i>=1;i--)

        {

            b[ans]+=t*a[i];

            t*=10;

        }

        */

    }

    else

    {

        for (i=1;i<=n;i++)

            if (vis[i])

            {

                vis[i]=false;

                a[pos]=i;

                dfs(pos+1);

                vis[i]=true;

            }

    }

}

 

int main()

{

    long i;

    cin>>n;

    for (i=1;i<=n;i++)

    {

        vis[i]=true;

        a[i]=i;

    }

    dfs(1);

    /*

    cout<<endl;

    for (i=1;i<=ans;i++)

        cout<<b[i]<<endl;

 

    cout<<endl;

    for (i=1;i<ans;i++)

        cout<<b[i+1]<<" - "<<b[i]<<" = "<<b[i+1]-b[i]<<endl;

    */

    return 0;

}

 

III.

从小到大排序,由第k个数推出第(k+1)个数

#include <iostream>

#include <sys/timeb.h>

using namespace std;

 

int main()

{

    long n;

    cin>>n;

    struct timeb t1,t2;

    ftime(&t1);

    long a[100],i,j,temp,x,y;

    for (i=1;i<=n;i++)

        a[i]=i;

    i=n-1;

    while (i)

    {

        for (j=1;j<=n;j++)

            cout<<a[j]<<" ";

        cout<<endl;

        //126543    -->132456

        //136542    -->142356

        //146532    -->152346

        for (j=n;j>=1;j--)

            if (a[j]>a[i])

                break;

        temp=a[i];

        a[i]=a[j];

        a[j]=temp;

        //i+1~n

        x=i+1;

        y=n;

        while (x<y)

        {

            temp=a[x];

            a[x]=a[y];

            a[y]=temp;

            x++;

            y--;

        }

        for (i=n-1;i>0;i--)

            if (a[i]<a[i+1])

                break;

    }

    for (j=1;j<=n;j++)

        cout<<a[j]<<" ";

    cout<<endl;

    ftime(&t2);

    cout<<(t2.time-t1.time)+(t2.millitm-t1.millitm)/1000.0<<endl;

    return 0;

}

 

IV.

康拓展开,通过数的编号逆推出数

#include <iostream>

using namespace std;

 

int main()

{

    long n,value[15],i,j,k,s,num;

    bool vis[15];

    cin>>n;

    value[0]=0;

    value[1]=1;

    for (i=2;i<=n;i++)

        value[i]=value[i-1]*i;

    for (i=0;i<value[n];i++)

    {

        for (j=1;j<=n;j++)

            vis[j]=false;

        s=i;

        for (j=n-1;j>=0;j--)

        {

            num=s/value[j];

            s-=num*value[j];

            k=1;

            while (vis[k])

                k++;

            while (num)

            {

                num--;

                k++;

                while (vis[k])

                    k++;

            }

            vis[k]=true;

            cout<<k<<" ";

        }

        cout<<endl;

    }

    return 0;

}

 

 

康拓展开

s[1],s[2],…,s[n]都不相同,且{s[1],s[2],…,s[n]}={1,2,…,n}

   X=(s[1],s[2],…,s[n])

Number(X)=a[n]*(n-1)!+a[n-1]*(n-2)!+...+a[1]*0!

a[i]:数s[i]比s[i+1]~s[n]大的个数或者s[i]-1 减去s[i]比s[1]~s[i-1]大的数目

(因为n个数从1~n且都不相同,比s[i]小的数有s[i]-1个)

   如X=2413

   Number(2413)=1*3!+2*2!+0*1!+0*0!=10

求一个数X的编号

Code:

#include <iostream>

using namespace std;

 

int main()

{

    long n,i,j,s[11],value[11],g,ans=0;

    cin>>n;

    for (i=1;i<=n;i++)

        cin>>s[i];

    value[0]=0;

    value[1]=1;

    for (i=2;i<n;i++)

        value[i]=value[i-1]*i;

    for (i=1;i<=n;i++)

    {

 

        g=0;

        for (j=i+1;j<=n;j++)

            if (s[i]>s[j])

                g++;

 

        /*

        g=s[i]-1;

        for (j=1;j<i;j++)

            if (s[i]>s[j])

                g--;

        */

        ans+=g*value[n-i];

    }

    cout<<ans<<endl;

    return 0;

}

 

Code:(高精度)

#include <iostream>

#include <memory.h>

using namespace std;

 

long max(long a,long b)

{

    if (a>b)

        return a;

    else

        return b;

}

 

int main()

{

    long n,i,j,s[100],value[100][100],len[100],g,ans[100],lenans,c[100],lenc;

    cin>>n;

    for (i=1;i<=n;i++)

        cin>>s[i];

    value[0][1]=0;

    len[1]=1;

    value[1][1]=1;

    len[1]=1;

    for (j=2;j<n;j++)

    {

        value[j][1]=0;

        for (i=1;i<=len[j-1];i++)

        {

            value[j][i]+=value[j-1][i]*j;

            if (value[j][i]>=10000)

            {

                value[j][i+1]=value[j][i]/10000;

                value[j][i]=value[j][i]%10000;

            }

            else

                value[j][i+1]=0;

        }

        if (value[j][i]==0)

            len[j]=len[j-1];

        else

            len[j]=len[j-1]+1;

    }

    /*

    value[0]=0;

    value[1]=1;

    for (i=2;i<n;i++)

        value[i]=value[i-1]*i;

    */

    lenans=1;

    memset(ans,0,sizeof(ans));

    for (i=1;i<=n;i++)

    {

        g=0;

        for (j=i+1;j<=n;j++)

            if (s[i]>s[j])

                g++;

        //ans+=g*value[n-i];

        c[1]=0;

        for (j=1;j<=len[n-i];j++)

        {

            c[j]+=g*value[n-i][j];

            if (c[j]>=10000)

            {

                c[j+1]=c[j]/10000;

                c[j]=c[j]%10000;

            }

            else

                c[j+1]=0;

        }

        if (c[j]==0)

            lenc=j-1;

        else

            lenc=j;

        lenans=max(lenans,lenc);

        for (j=1;j<=lenans;j++)

        {

            ans[j]+=c[j];

            if (ans[j]>=10000)

            {

                ans[j+1]++;

                ans[j]-=10000;

            }

        }

        if (ans[j]!=0)

            lenans++;

    }

    //cout<<ans<<endl;

    cout<<ans[lenans];

    for (i=lenans-1;i>=1;i--)

        if (ans[i]>=1000)

            cout<<ans[i];

        else if (ans[i]>=100)

            cout<<"0"<<ans[i];

        else if (ans[i]>=10)

            cout<<"00"<<ans[i];

        else

            cout<<"000"<<ans[i];

    cout<<endl;

    return 0;

}

 

Input:

 

100

100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61

 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21

 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

 

Output:

93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916863999999999999999999999999

 

全排序与康拓展开

标签:n+1   swa   output   memory   排列   [1]   bsp   个数   高精度   

原文地址:http://www.cnblogs.com/cmyg/p/6219358.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!