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

数字排列问题

时间:2016-08-06 11:20:55      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:

贵有恒,何必三更起五更眠;最无益,莫过一日曝十日寒。

数字排列问题

时间限制: 1 Sec  内存限制: 128 MB

题目描述

列出所有从数字1到数字n的连续自然数的排列,要求所产生的任一数字序列中不允许出现重复的数字。

输入

n(1≤n≤9)

输出

由1~n组成的所有不重复的数字序列,每行一个序列。

样例输入

3

样例输出

    1    2    3
    1    3    2
    2    1    3
    2    3    1
    3    1    2
    3    2    1

用了next_permutation()函数字典序排列,对应的有一个排序顺序相反的函数prev_permutation()

技术分享
#include <iostream>
#include <algorithm>

using namespace std;
int n;
int l[10];

int main()
{
    cin>>n;
    for(int i=0;i<=n;i++)
        l[i-1]=i;
    do
    {
        for(int i=0;i<n;i++)
            cout<<"    "<<l[i];
        cout<<endl;
    }while(next_permutation(l,l+n));
    return 0;
}
View Code

 


数字排列问题

标签:

原文地址:http://www.cnblogs.com/asuml/p/5743456.html

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