2018年NOIP提高组初赛C++阅读程序题:
#include<cstdio> using namespace std; const int N =110; bool isUse[N]; int n, t; int a[N], b[N]; bool isSmall() { for (int i = 1; i <= n; ++i) if (a[i] != b[i]) return a[i] < b[i]; return false; } boolgetPermutation(int pos) { if (pos > n) { return isSmall(); } for (int i = 1; i <= n; ++i) { if (!isUse[i]) { b[pos] = i; isUse[i] = true; if (getPermutation(pos + 1)) { return true; } isUse[i] = false; } } return false; } void getNext() { for (int i = 1; i <= n; ++i) { isUse[i] = false; } getPermutation(1); for (int i = 1; i <= n; ++i) { a[i] = b[i]; } } int main() { scanf("%d%d", &n, &t); for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); } for (int i = 1; i <= t; ++i) { getNext(); } for (int i = 1; i <= n; ++i) { printf("%d", a[i]); if (i == n) putchar('\n'); else putchar(''); } return 0; }
输入1:6 10 1 6 4 5 32
输出 1:_______
输入2:6 200 1 5 3 4 26
输出 2:_______