(According to the basic principles of Chinese chess) on the double-only handsome board, find all the positions where both sides can fall (the handsome guy cannot meet each other), but only one variable can be used.
Intuitively, we think that we only need to traverse all possible positions of the handsome guy and remove the conflicting positions of the handsome guy. It can be seen that the remaining problem is how to use a variable to traverse a double loop. The solution in the book is to split a Byte variable into two, and the first half represents the position where "handsome" can go, the next variable represents the location where "yes" can be taken (the positions 3*3 that "yes" and "handsome" can be taken are numbered in advance ), the bitwise operation can be used to obtain the functions of two counters.
The solution in the book is structured, and the bit field in the C language is used to solve the problem of traversing the double loop of a variable.
Solution 2 is really interesting.
(1)
Let's first look at algorithm 3:
for(i.a = 1; i.a <= 9; ++i.a) {for(i.b = 1; i.b <= 9; ++i.b) {if(i.a % 3 == i.b % 3)printf("A = %d, B = %d\n", i.a, i.b);}}
(2)
Next let's look at the most interesting algorithm 2:
BYTE i = 81;while(i--) {if(i / 9 % 3 == i % 9 % 3) continue;printf("A = %d, B = %d\n", i / 9 + 1, i % 9 + 1);}In the above algorithm, "/" and "%" are common tricks to use a variable value for internal and external layer loops.
In the process from 81 to 0, the change of I/9 is equivalent to the change of the outer loop, while the change of I % 9 is equivalent to the change of the inner layer.
1.2 Chinese chess masters