The beauty of programming Reading Notes 01: 1.2 Chinese chess masters

Source: Internet
Author: User

The beauty of programming Reading Notes 01: 1.2 Chinese chess masters

When I first saw the question, I first thought of Division calculation of the quotient and remainder of the divisor (eax and EDX ). Later I thought that the C/C ++ needs a dynamic multi-dimensional array, and the new one-dimensional array is used to simulate the conversion of the subscript and actual offset of the multi-dimensional array, I think many people have done this when learning C or C ++.

I thought that the question requires the specific location of the handsome guy on the Board, such as D10 and F1. But the solution in the book is relative location, which makes it easier to solve. The solution uses a bunch of annoying macros,CodeIt is really not beautiful. solution 3 and solution 1 are essentially the same. Although solution 3 defines a struct, there are two variables in the structure, and there are three variables in total.

To split a variable I into two, you can extract several consecutive bits based on its binary representation, such as the 0-3 and 4-7 bits, take out the corresponding digits of variable I. When saving the variable, update the corresponding digits of variable I. In addition, the location symmetry can be used to output two records at a time to reduce the number of cycles.

The following code is similar to the solution, but two are output at a time, which reduces the number of loops and does not use division. If the effect of C ++ I/O efficiency is not considered, it is more efficient than solution 2 and solution 3.

 

  // use the 4-7 position of I in the outer loop variable B. The initial value is 1 and the maximum value is 8.   // use the 0-3 bits of I in the inner loop variable a. The initial value is B + 1 and the maximum value is 9.   for  ( unsigned  I = 0x10; I <0x90; I + = 0x10)  for  (I = (I & 0xf0) | (I> 4); (++ I & 0xf) <10 ;)  If  (I & 0xf)-(I> 4 ))! = 3 & (I & 0xf)-(I> 4 ))! = 6) STD: cout <" A = " <(I> 4) <", B = " <(I & 0xf) <" \ n " <" A = " <(I & 0xf) <", B =  "<(I> 4) <"  \ n  "; 

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.