
这是一个基于数组的谜题,需要你将包含两个元素的数组中的所有数字都更改为0。数组的一个元素是0,另一个元素可能是0也可能不是。
要解决这个谜题,程序需要找到非零元素并将其更改为0。
以下是解决布尔数组谜题所需的约束条件 −
- 允许的操作是补集,其他操作不允许。
- 不允许使用循环和条件语句。
- 也不允许直接赋值。
解决布尔数组谜题的程序
#include <iostream>
using namespace std;
void makeZero(int a[2]) {
a[ a[1] ] = a[ !a[1] ];
}
int main() {
int a[] = {1, 0};
makeZero(a);
cout<h2>输出</h2>
<pre class="brush:php;toolbar:false;">arr[0] = 0
arr[1] = 0
You can use other ways too. Like this one which does not require the negation operation.
a[ a[1] ] = a[ a[0] ]
13万字C语言保姆级教程(深入):立即使用
在学习笔记中,你将探索c语言的核心概念和高级技巧!











