Serial lock Gray Code knowledge + large number

Source: Internet
Author: User

Serial lock

Title Abstract: One gray code to another gray code the minimum number of conversion steps required.

Idea: Directly find the corresponding decimal, subtract the absolute value.

When n=2 , the sequence of transformations is xx,

The sequence of transformations when n=3 is 001,011,010,111, 101,

......

Careful observation found that this is the gray code, adjacent to two states only one difference (in fact, the title description has said this condition)!

Decimal number

Natural binary Number

Gray Code

0

0000

0000

1

0001

0001

2

0010

0011

3

0011

0010

4

0100

0110

5

0101

0111

6

0110

0101

7

0111

0100

8

1000

1100

9

1001

1101

10

1010

1111

11

1011

1110

12

1100

1010

13

1101

1011

14

1110

1001

15

1111

1000

Ps:

Generally, the ordinary binary code and gray code can be converted to each other in the following ways:

Binary code-- Gray Code (code): From the rightmost one, and then each one with the left one XOR (XOR), as the corresponding Gray code that bit value, the leftmost one unchanged (equivalent to the left is 0);

Gray Code ---binary code (decoding): From the second left, each bit with the left one decoded value XOR, as the bit decoded value (the leftmost one remains unchanged).

1 ImportJava.io.*;2 Importjava.math.*;3 ImportJava.util.*;4 5 classMain6 {7      Public Static intms=128;8      Public StaticBiginteger[] Base=NewBiginteger[ms];9      Public Static voidInit ()Ten     { Onebase[0]=Biginteger.one; A          for(inti=1;i<128;i++) -Base[i]=base[i-1].multiply (Biginteger.valueof (2)); -     } the      Public Static voidMain (string[] args) -     { -Scanner cin=NewScanner (NewBufferedinputstream (system.in)); - init (); +         intt=cin.nextint (); -         intN; +         int[] A=New int[MS]; A         int[] b=New int[MS]; at          while(t-->0) -         { -n=cin.nextint (); -              for(inti=0;i<n;i++) -             { -a[i]=cin.nextint (); in                 if(i!=0) -a[i]=a[i-1]^A[i]; to             } +              for(inti=0;i<n;i++) -             { theb[i]=cin.nextint (); *                 if(i!=0) $b[i]=b[i-1]^B[i];Panax Notoginseng             } -BigInteger s=Biginteger.zero; theBigInteger t=Biginteger.zero; +              for(inti=0;i<n;i++) A             { the                 if(a[n-1-i]!=0) +s=S.add (Base[i]); -                 if(b[n-1-i]!=0) $t=T.add (Base[i]); $             } - System.out.println (T.max (s). Subtract (T.min (s))); -         } the cin.close (); -     }Wuyi}

Serial lock Gray Code knowledge + large number

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.