例如下面的内容:
| 0 1 0 1 0 |
| 1 0 1 0 1 |
| 0 1 0 1 0 |
| 1 0 1 0 1 |
| 0 1 0 1 0 |
以下是一个好的方法,但这仍然不会产生所需的输出:
void AltBitArray(int array[][4], int size)
{
int row, column;
int val = 0;
for (row = 0; row < size; row++) {
for (column = 0; column < size; column++) {
if (val == 0) {
array[row][column] = val;
val = 1;
}
else
{
array[row][column] = val;
val = 0;
}
}
}
}
答案 0 :(得分:0)
Y会将详细代码转换为以下内容:
for (row = 0; row < size; row++) {
for (column = 0; column < size; column++) {
if (val == 0) {
array[row][column] = val;
val = 1;
}
else
{
array[row][column] = val;
val = 0;
}
}
}
进入以下
for (row = 0; row < size; row++) {
for (column = 0; column < size; column++) {
array[row][column] = val;
val = val ^ 1; // or with val = 1 - val;
}
}
这是一个有效的演示 https://ideone.com/AEpRjH
// Output
0 1 0 1 0
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1
0 1 0 1 0