The beauty of programming-question about Chinese chess masters

Source: Internet
Author: User

The question requires that all valid locations of "" (a) and "" (B) be output, and only one byte storage variable can be used;

Solution 1:

 1 #include<iostream> 2 using namespace std; 3  4 int main() 5 { 6     int i=81; 7     while(i--) 8     { 9         if(i/9%3==i%9%3)10             continue;11         else12             cout<<i/9+1<<‘\t‘<<i%9+1<<endl;13     }14     return 0;15 }
View code

There are nine methods for a and B, So I = 81, for an integer I = I/9*9 + I % 9; I % 9 with I/9 change round, it is equivalent to a nested loop; an I is used to represent the changes of two values;

Of course, you can also use this method to implement multiple nesting;

Solution 2:

 1 #include<iostream> 2 using namespace std; 3  4 struct myBite 5 { 6     unsigned char a:4; 7     unsigned char b:4; 8 }; 9 10 int main(void)11 {12     myBite i;13     for(i.a=1;i.a<=9;++i.a)14         for(i.b=1;i.b<=9;++i.b)15             if(i.a%3!=i.b%3)16                 cout<<(int)i.a<<‘\t‘<<(int)i.b<<endl;17     return 0;18 }
View code

Refer to related bit operations

 

The beauty of programming-question about 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.