D的小L
时间限制:
4000 ms | 内存限制:
65535 KB
难度:
2
-
描述
-
一天TC的匡匡找ACM的小L玩三国杀,但是这会小L忙着哩,不想和匡匡玩但又怕匡匡生气,这时小L给匡匡出了个题目想难倒匡匡(小L很D吧
),有一个数n(0<n<10),写出1到n的全排列,这时匡匡有点囧了
,,,聪明的你能帮匡匡解围吗?
-
输入
- 第一行输入一个数N(0<N<10),表示有N组测试数据。后面的N行输入多组输入数据,每组输入数据都是一个整数x(0<x<10) 输出
- 按特定顺序输出所有组合。 特定顺序:每一个组合中的值从小到大排列,组合之间按字典序排列。 样例输入
-
2 2 3
样例输出
-
12 21 123 132 213 231 312 321
STL容器做法较为简单:
只需要一个简单的向下的容器:字典序排列
next_permutation(a+start,a+end);
代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 using namespace std; 5 int main() 6 { 7 int n,t,i; 8 int str[]={1,2,3,4,5,6,7,8,9,10}; 9 scanf("%d",&t); 10 while(t--) 11 { 12 scanf("%d",&n); 13 do{ 14 for(i=0;i<n;i++) 15 printf("%d",str[i]); 16 puts(""); 17 18 } 19 while(next_permutation(str,str+n)); 20 21 /* while(prev_permutation(str,str+n)) 22 { 23 for(i=0;i<n;i++) 24 printf("%d",str[i]); 25 puts(""); 26 27 } 28 */ 29 30 31 } 32 return 0; 33 }