For reprinting, and bitwise operations

Source: Internet
Author: User

 

This is a homework assignment assigned by the math model teacher. I found that the C language can be used to solve the problem well, but it is a pity that the commodity teacher cannot understand it! What a sad word !!!

Question:

A total of eight pawns are taken out of black and white, which are arranged in a circle. then, place a Black Pawn among the two chess pieces in the same color, and place a white pawn among the two chess pieces in different colors. repeat the above process, so that we can put down a circle and take away the previous circle of pawns. Then, how can we change the color of each piece in this way?

 

My algorithms:

There are eight black and white pawns. 0 indicates black and 1 indicates white. Question: Black and black are playing black, white are playing black in the middle, and white is playing white in the middle of black and white. Bitwise operations (exclusive or) can be used ).
The unsigned char is exactly 8 bits. It can simulate the black and white States and can enumerate all cases (256 types ).
A number A indicates a state, and then shifts the value to TA in a loop. A ^ TA can get the status of the next eight pieces, when a status is the same as the previous status, the result is obtained and the loop is exited.

My code:

# Include <stdio. h> unsigned char left_one (unsigned char X); // shift one void color (unsigned char X) to the left of the loop ); // color int P [8] = {, 64, 32, 16,} according to the digital status; // The secondary array, to obtain whether a digit is 1 or 0int main () {int I, Count, flag; unsigned char a, B, Ta, TB; //, B Indicates the two statuses before and after for (I = 0; I <256; ++ I) // enumerative 256 cases {flag = 1; //, B State loop is based on a = I; B = I + 1; printf ("\ n initial state:"); color (a); Count = 0; while (! = B) {If (FLAG) {TA = left_one (a); B = a ^ ta; ++ count; flag = 0 ;}else {TB = left_one (B ); a = B ^ TB; ++ count; flag = 1 ;}// while () printf ("times used: % d \ n", Count-1 ); printf (" final state:"); color (a);} // For () getchar (); Return 0;} unsigned char left_one (unsigned char X) {unsigned char T = x <1; if (X & 128) ++ t; return t;} void color (unsigned char X) {int I; for (I = 0; I <8; ++ I) {If (X & P [I]) printf ("white"); else printf ("black ");} printf ("\ n ");}

 

Result:

Up to eight times, the result is stable, and the final result is black.

Feelings:

I spent 30 minutes coding and 3 hours debugging. It's worth it, because I don't feel I can continue to optimize it. Haha!

Bit operations are a good thing! Efficient! Amazing!

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.