2 3 1 4 2
1 2 3 12 13 14 21 23 24 31 32 34 41 42 43
使用 C++ STL next_permutation();
#include<cstdio> #include<algorithm> #include<iostream> using namespace std; int N, n, m; int main() { //freopen("in.txt", "r", stdin); cin >> N; while(N--) { cin >> n >> m; string s1,s2; for (int i = 1; i <= n; i++) s1 += '0' + i; s2 = s1.substr(0, m); cout << s2 << endl; while(next_permutation(s1.begin(), s1.end())) { if (s2 != s1.substr(0, m)) { s2 = s1.substr(0, m); cout << s2 << endl; } } } return 0; }