全排列
Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 70 Accepted Submission(s) : 20
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
某天小宏在看八皇后算法,越看越困。为什么?原来解决八皇后问题,需要懂得全排列算法,而书本对全排列算法的分析太少,所以看得他糊里糊涂。为了解决八皇后,他唯有向编程高手的你求助了。
Input
输入整数 n,表示是n个数的全排列;接下一行从小到大输入n个整数(没有相同的两个数)。(1<= n <=8)
Output
按升序输出全排列(升序:对序列a1a2a3…an,b1b2b3…bn;若前k-1位相同,而第k位有ak < bk,则a序列小于b序列)
Sample Input
Sample Output
1 3 10 1 10 3 3 1 10 3 10 1 10 1 3 10 3 1
Author
#include <iostream>
#include <algorithm>
#include <cstdlib>
using namespace std;
#include <algorithm>
#include <cstdlib>
using namespace std;
int main()
{
int c;
int a[8];
cin>>c;
for(int i=0;i<c;i++)
scanf("%d",&a[i]);
sort(a,a+c);
do {
printf("%d",a[0]);
for(int i=1;i<c;i++)
printf(" %d",a[i]);
printf("/n");
}while(next_permutation(a,a+c));
{
int c;
int a[8];
cin>>c;
for(int i=0;i<c;i++)
scanf("%d",&a[i]);
sort(a,a+c);
do {
printf("%d",a[0]);
for(int i=1;i<c;i++)
printf(" %d",a[i]);
printf("/n");
}while(next_permutation(a,a+c));
system("pause");
return 0;
}
}
本文介绍了一个全排列算法的实现案例,通过C++代码演示如何生成指定数量整数的所有可能排列组合,并按照升序输出这些排列。
2926

被折叠的 条评论
为什么被折叠?



