1.2 Chinese chess masters

Source: Internet
Author: User

(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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.