Codeforces 535B-Tavas and SaDDas, codeforces
Assume that only a number consisting of 4 or 7 is a lucky number. Enter n and find the nth number in all lucky numbers.
Analysis: all lucky numbers are pre-processed, sorted, and then the corresponding position of n is output. The processing method is initial a [0] = 4, a [1] = 7, and then two digits are generated by adding 4 and 7 to the front of the two numbers, use ten digits to generate three digits, that is, 4-> 44 and-> 47 and 77. Because the n upper limit is 10 ^ 9, 9 digits are generated, and the outer loop is only 8, the inner loop is not large and will not time out. It is best not to use pow here, because pow is a floating point computation. Write a pow function by yourself.
PS: I handed it over several times and said that the fifth example was not output. I tested it myself, and others did. So I didn't get the AC, But I consulted Daniel, he said I was right, and I thought I was right.
Code:
# Include <iostream> # include <algorithm> # include <cmath> # define INF 1000000007 using namespace std; int n, a [10000002]; int k; void init () {a [0] = 4, a [1] = 7; int l = 0, r = 2; k = 2; for (int p = 1; p <9; p ++) {for (int I = l; I <r; I ++) {int tmp = 1; for (int j = 1; j <= p; j ++) tmp * = 10; // This loop is used to implement the pow function. pow is a floating-point number operation, which is not used for integer operation, precision Problems // use a loop or a quick power to write a pow. The complexity is O (n) and O (logn) a [k ++] = 4 * tmp + a [I]; a [k ++] = 7 * tmp + a [I];} l = r, r = k;} sort (a, a + k);} int main () {init (); cin> n; for (int I = 0; I <k; I ++) if (a [I] = n) cout <I + 1 <endl ;}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.