我正在尝试理解教授教授排列的代码,但我不知道 if 语句中的“(!used[i])”是什么意思或做什么。这是完整的函数,if 语句位于 for 循环内。谁能解释一下它的作用吗?
void RecursivePermute(int n, int k, int* perm, int* used) {
int i;
// We've filled perm already. Print the corresponding permutation.
if (k == n) {
for (i=0; i<n; i++)
printf("%d ", perm[i]);
printf("\n");
}
// Try each unused number in spot k.
for (i=0; i<n; i++) {
if (!used[i]) { //this if statement is my question
perm[k] = i;
used[i] = 1;
RecursivePermute(n, k+1, perm, used);
used[i] = 0;
}
}
}
最佳答案
表示not,因此当int
元素used[i] == 0
时会触发if
语句code>,所以也可以写成:
if (used[i] == 0) {
...
}
关于c - 在 if 语句中使用数组旁边的感叹号意味着什么? "if (!used[i])",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18745830/