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