nyoj-366-D的小L(求全排列)

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
来源
原创
上传者
kapop
 1 /*
 2  * http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=366
 3  * by jtahstu on 2015/2/5 4:00
 4  * 知识点: 排列生成器     按字典序的下一个排列     next_permutation()
 5  *                      按字典序的前一个排列     prev_permutation()
 6  */
 7 
 8 #include <iostream>
 9 #include <algorithm>
10 #include <string>
11 using namespace std;
12 
13 int main() {
14     int n;
15     cin>>n;
16     while(n--)
17     {
18         int m;
19         string s;
20         cin>>m;
21         for(int i=1;i<=m;i++)
22             s+=i+'0';
23         cout<<s<<endl;
24         while(next_permutation(s.begin(),s.end()))
25             cout<<s<<endl;
26     }
27     return 0;
28  }

 3 #include<iostream>
 4 #include<algorithm>
 5 using namespace std;
 6 int a[]={1,2,3,4,5,6,7,8,9};
 7 int main()
 8 {
 9     int n,r;
10     cin>>r;
11     while(r--)
12     {
13      cin>>n;
14      do
15      {
16      for(int i=0;i<n;i++)
17          cout<<a[i];
18      cout<<endl;
19      }while(next_permutation(a,a+n));
20     }
21  return 0;
22 }        
原文地址:https://www.cnblogs.com/jtahstu/p/4273811.html