【OpenJ_Bailian - 4070 】全排列
全排列
Descriptions:
对于数组[1, 2, 3],他们按照从小到大的全排列是
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
现在给你一个正整数n,n小于8,输出数组[1, 2, …,n]的从小到大的全排列。
Input
输入有多行,每行一个整数。当输入0时结束输入。
Output
对于每组输入,输出该组的全排列。每一行是一种可能的排列,共n个整数,每个整数用一个空格隔开,每行末尾没有空格。
Sample Input
2
3
0
Sample Output
1 2
2 1
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
题目链接:
https://vjudge.net/problem/OpenJ_Bailian-4070
讲一个STL函数:
int main()
{
int a[];
a[]=;
a[]=;
a[]=;
do
{
cout<<a[]<<""<<a[]<<""<<a[]<<endl;
}
while (next_permutation(a,a+)); //参数3指的是要进行排列的长度 //如果存在a之后的排列,就返回true。如果a是最后一个排列没有后继,返回false,每执行一次,a就变成它的后继 }
输出:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
如果改成 while(next_permutation(a,a+2));
则输出:
1 2 3
2 1 3
只对前两个元素进行字典排序
显然,如果改成 while(next_permutation(a,a+1)); 则只输出:1 2 3
若排列本来就是最大的了没有后继,则next_permutation执行后,会对排列进行字典升序排序,相当于循环
int list[3]={3,2,1};
next_permutation(list,list+3);
cout<<list[0]<<""<<list[1]<<""<<list[2]<<endl;
//输出: 1 2 3
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <numeric>
using namespace std;
typedef long long ll;
int n;
int main()
{
while(cin >> n && n!=)
{
int a[];
for(int i=; i<n; i++)
a[i]=i+;
int j;
do
{
for(int i=; i<n; i++)
{
// 输出格式,特殊处理
if(i==n-)
cout<<a[i];
else
cout <<a[i]<< " ";
}
cout << endl;
}
while(next_permutation(a,a+n));
}
}
【OpenJ_Bailian - 4070 】全排列的更多相关文章
- PHP实现全排列(递归算法)
算法描述:如果用P表示n个元素的全排列,而Pi表示n个元素中不包含元素i的全排列,(i)Pi表示在排列Pi前面加上前缀i的排列,那么n个元素的全排列可递归定义为: ① 如果n=1,则排列P只有一 ...
- hdu5651 xiaoxin juju needs help (多重集的全排列+逆元)
xiaoxin juju needs help 题意:给你一个字符串,求打乱字符后,有多少种回文串. (题于文末) 知识点: n个元素,其中a1,a2,··· ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [LeetCode] Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] Permutations 全排列
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- 全排列算法的JS实现
问题描述:给定一个字符串,输出该字符串所有排列的可能.如输入“abc”,输出“abc,acb,bca,bac,cab,cba”. 虽然原理很简单,然而我还是折腾了好一会才实现这个算法……这里主要记录的 ...
- java实现全排列
前天上午的面试遇到了一个用java实现一串数字的全排列的题,想来想去用递归最方便,可是没有在规定的时间内完成555,今天上午有空便继续写,以下是完成后的代码: import java.util.Arr ...
- poj3187-Backward Digit Sums(枚举全排列)
一,题意: 输入n,sum,求1~n的数,如何排列之后,相邻两列相加,直到得出最后的结果等于sum,输出1~n的排列(杨辉三角) 3 1 2 4 //1~n 全排列中的一个排列 4 3 6 7 ...
随机推荐
- controller跳到另一个controller
1.无参数: return "redirect:park/findByTag"; 2/有参数: public String addChild(Model model2) model ...
- Fedora20 安装 MySQL
参考资料: http://www.cnblogs.com/focusj/archive/2011/05/09/2057573.html http://linux.chinaunix.net/techd ...
- A. Link/Cut Tree--cf614A ()
这个题卡精度了 刚开始是2的57次方都已经有误差了 不知道怎么弄 后来加个求余就好了 #include<stdio.h> #include<math.h> #include ...
- java构造方法的特点和理解--三只坚果
构造方法的特点:1.首先构造方法是基于类,名字必须与类的名字完全相同(构造方法一般是自己编写的类需要初始化)2.每个类都有一个默认的构造方法,既无参数又无返回值,其作用是使用new操作符创建新对象后初 ...
- 一个动态链接的问题,dlsym后符号调用主函数的符号报告无法找到
先看看状况(小心头疼) client.c 编译得到 client:在 client 的 main 中用 dlopen( "./liba.so", RTLD_LAZY|RTLD_GL ...
- jquery显示和隐藏元素
1.$('#id').show()/$('#id').hide()/$('#id').toggle() 2.$('#id').css('display','none')/$('#id').css('d ...
- 【scrapy】Item及Spider
Items Item objects are simple containers used to collect the scraped data.They provide a dictionary- ...
- Centos7 Samba 独立账户
创建了一个组:smbgrp 和用户srijan通过认证来访问Samba服务器. groupadd smbgrp useradd srijan -G smbgrp smbpasswd -a srijan ...
- DosBox 报错 this program requires dosxnt.exe to be in your path
也就是少了dosxnt.exe文件,能够上网搜索下载,把dosxnt 拷贝到你挂截文件夹下就能够执行 Dosxnt文件下载
- bufferevent 与 socket
http://blog.sina.com.cn/s/blog_56dee71a0100qx4s.html 很多时候,除了响应事件之外,应用还希望做一定的数据缓冲.比如说,写入数据的时候,通常的运行模式 ...