【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 ...
随机推荐
- 牛客网 牛客网暑期ACM多校训练营(第三场)E KMP
链接:https://www.nowcoder.com/acm/contest/141/E 题目描述 Eddy likes to play with string which is a sequenc ...
- Unable to process request: General SSLEngine problem.Unable to connect to neo4j at `localhost:7687`, because the certificate the server uses has changed.
Exception in thread "main" org.neo4j.driver.v1.exceptions.ClientException: Unable to proce ...
- SpringMVC get请求中文乱码
针对GET请求的编码问题,则需要改tomcat的server.xml配置文件,如下: 原 <Connector connectionTimeout="20000" port= ...
- Meteor表单
在本教程中,我们将告诉你如何使用 Meteor 的表单. 文本输入 首先,我们将创建一个 form 元素中文本输入字段和提交按钮. meteorApp/import/ui/meteorApp.html ...
- 从JVM的角度看JAVA代码--代码优化
从JVM的角度看JAVA代码–代码优化 从JVM的角度看JAVA代码代码优化 片段一反复计算 片段二反复比較 在JVM载入优化为class文件,运行class文件时,会有JIT(Just-In-Tim ...
- VB和VB.NET有什么区别
作为VB6.0的后续版本,VB.NET更加稳定,而且完全面向对象.也许你还记得,VB6.0部支持继承.重载和接口,所以不是真正面向对象的.而VB.NET则支持这些面向对象特性.VB6.0有两个薄弱环节 ...
- 项目实战之玩转div+css制作自己定义形状
项目需求 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/diss ...
- poj 2585 Window Pains 暴力枚举排列
题意: 在4*4的格子中有9个窗体,窗体会覆盖它之下的窗体,问是否存在一个窗体放置的顺序使得最后的结果与输入同样. 分析: 在数据规模较小且不须要剪枝的情况下能够暴力(思路清晰代码简单),暴力一般分为 ...
- 将C++的标识符转成OC的标识符
3.将C++的标识符转成OC的标识符 C++的标识符和OC一样由数字字母下划线组成.打头的不是数字.当标识符超过一个单词, C++採用全字母小写.单词间用下划线连接的书写规范,如: bei_jing ...
- base64和图片互转
pom.xml添加 <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec --> <dependen ...