码迷,mamicode.com
首页 > 其他好文 > 详细

全排列(STL)

时间:2015-05-19 13:07:43      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:

#include <iostream>
#include <algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

using namespace std;
int n,m;

void per(char* list, int length)
{
    sort(list, list + length);
    do
    {
        for(int i = 0; i < length; i++)
        {
            if(i == length-1)
            {
                cout << list[i];
            }
            else
            {
                cout << list[i] << " ";
            }
        }
        cout << endl;
    }while(next_permutation(list, list + length));
}

int main()
{
    char a[2100];
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=0;i<n;i++)
        {
            a[i] = (i+1)+'0';
        }
        a[n] = '\0';
        per(a,(int)strlen(a));
    }

    return 0;
}
<pre name="code" class="cpp">

#include <iostream>
#include <algorithm>
#include <iterator>
#include<stdio.h>
#include<string.h>

using namespace std;

int a[1001];
int n,m;

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=0;i<n;i++)
        {
            a[i] = i+1;
        }
        do
        {
            for(int i=0;i<n;i++)
            {
                if(i == n-1)
                {
                    printf("%d\n",a[i]);
                }
                else
                {
                    printf("%d ",a[i]);
                }
            }
        }
        while( next_permutation(a,a+n));
    }

    return 0;
}




全排列(STL)

标签:

原文地址:http://blog.csdn.net/yeguxin/article/details/45842807

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