看了其他人的题解,感觉都挺复杂。我来个简单易懂的,五重循环搞定。
#include <iostream> using namespace std; int main() { int k; cin >> k; for (int a = 9; a >= 0; --a) { for (int b = 9; b >= 0; --b) { if (a == b) continue; for (int c = 9; c >= 0; --c) { if (a == c || b == c) continue; for (int d = 9; d >= 0; --d) { if (a == d || b == d || c == d) continue; for (int e = 9; e >= 0; --e) { if (a == e || b == e || c == e || d == e) continue; if (--k == 0) { cout << a << b << c << d << e; } } } } } } } // 64 位输出请用 printf("%lld")