D的小L
-
描述
-
一天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
-
#include <cstdio> #include <algorithm> #include <cstring> #define MAX 100
using namespace std;
int main() { int length; int test; scanf("%d",&test); while(test--) { int n; scanf("%d",&n); char str[MAX]; memset(str,NULL,sizeof(str)); for(int i=0; i<n; i++) { char c='0'+i+1; str[i]=c; } length = strlen(str); sort(str, str + length); puts(str); while (next_permutation(str, str + length)) { puts(str); } } return 0; }
-
用这个函数 求集合的幂集就很easy了
s2=s1.substr(0,y); cout<<s2<<endl; while(next_permutation(s1.begin(),s1.end())) { if(s2!=s1.substr(0,y)) { s2=s1.substr(0,y); cout<<s2<<endl; } }