Move 2 to the front, the figures become twice

zhaozj2021-02-16  71

Some people have such a number in the forum, asking for a number, the number of bit numbers is 2, when moving 2 on the 2nd place, this number is twice the number of ranules.

At first I used a loop, no fruit, it is estimated that this number may be very huge. After the analysis algorithm, it can be simplified:

#include using namespace std; int main (void) {__INT64 a = 1, b = 2; int P; while (true) {ix ((p = (int) (A - (A / 10) * 10 )) == 2 && b% a == 0) {cout << a << endl; break;} a = (b = b * 10 p) / 2;} system ("pause"); return 0; } The algorithm is explained as follows:

It is set to be evaluated, and the integer b is made, so that the value of B is equal to the value of the highest bit, and A satisfies two conditions: 1. A number of bit numbers 22. B = 2A

Algorithm E1. Initial A is 1, B is 2

E2. Queu A number of bit numbers and assignes P

E3. If P = 2 shows a first condition that satisfies the number, if B can be removed, the second condition is satisfied, then the number, the output result, exit the program.

E4. If you do not meet the above two conditions, then the order B shifts one bit based on 10 feed, and assigns a bit to P. (Give Point to P is to satisfy "the value of B is equal to the value of the one-shift movement to the highest bit")

E5. Let a = B / 2

E6. Jump to E2 to continue judgment.

However, the algorithm is not universal, the number is very huge, and __INT64 must be used to support the operation, and the efficiency is low. Optimized again to obtain a pure C code algorithm.

#include #include int main (void) {INT Q = 2, a = 1; While (1) {a = q / 2; Printf ("% d", a); IF (q == 4) Break; q = a (q% 2) * 10;} return 0;}

转载请注明原文地址:https://www.9cbs.com/read-18395.html

New Post(0)