Codeforces 535B-Tavas and SaDDas, codeforces

Source: Internet
Author: User

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.

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.