The beauty of programming: Chinese chess masters

Source: Internet
Author: User

The beauty of programming: Chinese chess masters
I. Differences between interview questions and acm questions.

I have been reading interview questions over the past few days, and I feel different from my previous questions on acm. In terms of subject matter, acm generally chooses generic algorithms that seldom involve local APIs and other things due to different client systems. At the same time, acm seldom limits the code content, it requires both time complexity and space complexity. The interview questions are made on dedicated machines without standardized input and output, as long as you can run the test, but the interviewer can make more requirements on your code, for example, only one variable is used, library functions are not used, and underlying APIs of a system are used. This makes our interview quite different from acm at ordinary times. After reading the questions above leetcode, I felt a little like the taste of the interview question, but I don't know how they got the correct answer, it seems like a good platform.

Ii. bitwise operations and macro definition setting Data Structure Method

The following method divides A char variable (eight digits) into four digits and four digits. the last four digits indicate the location of A and the next four digits indicate the location of B, obviously, the output here is user-defined, and there is no requirement for output during the interview. You only need to provide a reasonable idea.

# Include <stdio. h> # define HALF_BITS_LENGTH 4 // This value is the average length of the memory unit. In this question, it is 4bit; # define FULLMASK 255 // This number indicates a full-bit mask, in binary representation, it is 11111111 # define LMASK (FULLMASK <HALF_BITS_LENGTH) // This macro represents a left-bit mask. In binary representation, it is 11110000, // if there is an operator in the macro, remember to add brackets # define RMASK (FULLMASK> HALF_BITS_LENGTH) // This macro represents a left bit mask, in binary representation, it is 00001111, # define RSET (B, n) (B = (LMASK & B) ^ n )) // set the macro to n; # define LSET (B, n) (B = (RMASK & B) ^ (n <HA LF_BITS_LENGTH) // This macro sets the left side of B to n; # define RGET (B) (RMASK & B) // This macro gets the value to the right of B; # define LGET (B) (LMASK & B)> HALF_BITS_LENGTH) // The macro gets the value to the left of B; # define GRIDW 3 // The width of the grid, // The above provides another way of thinking for us to encapsulate the data structure-use the macro definition/the above macro definition to set a data structure, the variable of this data structure is a BITE, // The operation is to assign values to the left half and right half, and to obtain the values of the left and right parts respectively; // here the abstract and hierarchical design is used. rset lset rget lget gridw is directly used as the Application Macro for calling. // lmask rmask is the second-level macro abstracted. FULLMASK and HASL_BITS_LENGTH are the underlying calls. // This design idea must be from top to bottom, rather than from bottom to top. Int main () {unsigned char B; // you can use unsigned to obtain an 8-digit number. for (LSET (B, 1); LGET (B) <= GRIDW * GRIDW; LSET (B, (LGET (B) + 1) for (RSET (B, 1); RGET (B) <= GRIDW * GRIDW; RSET (B, (RGET (B) + 1) if (LGET (B) % GRIDW! = RGET (B) % GRIDW) printf ("A = % d, B = % d \ n", LGET (B), RGET (B); return 0 ;}

 

Iii. Mathematical splitting

PosA * 9 + posB is retrieved (0-80) and each pair of positions corresponds to a unique value one by one. This is an important idea, map the relationship between two values to one coordinate axis.

In C, unsigned char can be used to represent an integer in a byte. Therefore, char is essentially an integer. Let's see, there is still no problem if data is output in reverse order.

#include <stdio.h>int main(){       unsigned char b = 81;       while(b--)       {              if(b/9%3!=b%9%3)                     printf("A=%d,B=%d\n",b/9%3+1,b%9%3+1);       }       return 0;} 

 

Iv. byte splitting

 

Here we use the character that struct shares bytes.

#include <stdio.h> int main(){       struct{              unsigned char a:4;              unsigned char b:4;        }i;             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);              //printf("A:%c%d B:%c%d\n",'d'+i/3,i%3+1);                    }        return 0;}

 

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.