输出全排列 (20 分)(next_permutation的应用)

请编写程序输出前n个正整数的全排列(n<10),并通过9个测试用例(即n从1到9)观察n逐步增大时程序的运行时间。

输入格式:
输入给出正整数n(<10)。

输出格式:
输出1到n的全排列。每种排列占一行,数字间无空格。排列的输出顺序为字典序,即序列a
​1,a​2,⋯,a​n​​ 排在序列b1,b​2​​ ,⋯,bn 之前,如果存在k使得a​1​​=b1,⋯,a​k=bk并且 ak+1 <bk+1 。

输入样例:
3
输出样例:
123
132
213
231
312
321
思路:应用next_permutation()库函数直接输出

#include <bits/stdc++.h>
using namespace std;
int main() {
	int n, a[12] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
	scanf("%d", &n);
	do{
		for(int i = 0; i < n; i++) {
			printf("%d", a[i]);
		}
		printf("\n");
	}while(next_permutation(a, a + n));
	return 0;
}

猜你喜欢

转载自blog.csdn.net/dream_it_/article/details/88357775