阅读程序 · 答案 2 1 3 5 6 43 2 5 6 1 4 · 考点:输入输出、分支结构、循环结构
← 整套速查表 NOIP 提高组初赛 全部真题阅读程序写结果:
#include <iostream>
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;
}
bool getPermutation(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) {
ge
(本题为阅读程序 / 完善程序,完整题目与测试数据见平台)程序回溯生成排列并判断条件(isSmall 比较字典序),输出满足条件的两个排列:2 1 3 5 6 4 和 3 2 5 6 1 4。
getPermutation 按位置逐个尝试 1~n,回溯时恢复 isUse;isSmall 按字典序比较 a 与 b。
程序生成排列并用 isSmall 判定,输出符合条件的两组排列: 2 1 3 5 6 4、3 2 5 6 1 4。