Hihocoder 1234 Fractal (Law of Search)

Source: Internet
Author: User

Topic Links:

http://hihocoder.com/problemset/problem/1234

Problem Solving Ideas:

Main topic:

With A0 (0,0), B0 (0,1), C0 (total), D0 (1,0) Four points of the square, each take the edge a0b0, B0C0, c0d0, d0a0 midpoint connected, can get a new square, press

This operation 1000 times, you can get with the title given to the picture, now in X for 0~0.5 between a vertical x=k, ask you the vertical line and the logo has a few intersections, when there are infinite

Output "-1" directly at the intersection of

Algorithm idea:

A closer look will reveal that the horizontal axis of each vertical edge is equal to (the horizontal axis of the previous vertical edge +0.5)/2, and then according to: if the vertical line is coincident with the edge of the small square,

The output is "1"; the other, the farther inward, each passing through a square with an edge perpendicular to the x-axis, adds 4 intersections. Finally, it can be solved by lower_bound ...

AC Code:

#include <iostream>
#include <cstdio>
using namespace std;

Double a[1005];

void Init () {
    double x = 0.5;
    for (int i = 0; I <=, i++) {
        a[i] = 0.5-x;
        x *= 0.5;
    }
}

int main () {
    int T;
    Init ();
    scanf ("%d", &t);
    while (t--) {
        double K;
        scanf ("%lf", &k);
        int pos = Lower_bound (a,a+505,k)-A;
        if (a[pos] = = k)
            puts ("-1");
        else
            printf ("%d\n", 4*pos);
    }
    return 0;
}


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.