c - 在 if 语句中使用数组旁边的感叹号意味着什么? "if (!used[i])"

标签 c arrays if-statement

我正在尝试理解教授教授排列的代码,但我不知道 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/

相关文章:

c - 为什么C中的递归会出现段错误

C 运算符结果类型

java - 内存不足错误 : Java heap space - Using big array size

javascript - 我如何在 mustache.js 中做一个 if with value?

c - 是否有符合 POSIX 标准的方法来获取我的计算机的本地网络 IP 地址?

c - 使用指针而不是索引更新数组

JavaScript for 循环 Index 变为 arrayIndex

if-statement - VBScript - 如何生成随机数,然后使用 If 语句使用该数字来选择选项

c++ - 检查 3 个成对字母的实例

c - 海斯 AT 命令 : Detect Remote Hangup?