NYOJ: Question 529 flip, nyoj question 529 flip

Source: Internet
Author: User

NYOJ: Question 529 flip, nyoj question 529 flip

Question link: http://acm.nyist.net/JudgeOnline/problem.php? Pid = 1, 529

This question has too many slots, so I have not endured it...
Time:

The pass rate was surprisingly high, and I came in happily to think about it......
Baidu was not familiar with the question in the discussion area at the beginning,
Fortunately, I used these fragmentary clues to answer the question below,
If you are able to access the AC but are not sure about the intention of the 484 question, I hope it will help you (will this help you? ^_^ ).

Question meaning:
To change the decimal x to x + 1 through the number (0 to 1, or 1 to 0) at any position of the binary of x, only one bit of the binary x can be changed at a time, the minimum number of times required. haha!

Solution:
The manual draft method is used to add fingers and quick mental computing on the paper (this is the Shenma algorithm 0.0 ~ 5. The process is described as follows:
1 (0001)-> 0011-> 0010-> 2 (0010) => change twice
2 (0010)-> 0011-> 3 (0011) => Change 1
3 (0011)-> 0111-> 0110-> 0100-> 4 (0100) => change to 3
4 (0100)-> 0101-> 5 (0101) => Change 1
5 (0101)-> 0111-> 0110-> 6 (0110) => change to 2
Haha, get the rule: the binary number of x starts from 1 and ranges from right to left. The first 0 position must be changed several times at least.
(Ps: the principle of being so long seems a bit like talking about it or not)
(Rule principle: the minimum number of changes to the binary value of x plus 1, including the number of places changed by carry, the binary plus the carry value of 1 Changes to the position where the binary number of x ranges from right to left, and the first 0 .)
Now I can write code. I 've been reading a lot of comics recently, and I have been spitting out so many times without knowing it. It's so rare that I can't waste writing it into my blog.

// For a boy like me, let's talk about the simple brute-force law. Don't worry about timeout. Haha, (after submission, AC said,)

1 # include <iostream> 2 using namespace std; 3 int main () {4 int T, x, n; 5 cin> T; 6 while (T --) {7 cin> x; 8 n = 0; 9 int y = x % 2; 10 do {11 y = x % 2; 12 x/= 2; 13 n ++; 14} while (y! = 0); 15 cout <n <endl; 16} 17}<Code implementation> click to expand

Started with: 2016.9.9 ---- Zhiyin

 

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.