Flexible application of Fibonacci thought (P1011 Los problem solution to a problem Valley, Java language description)

Questions asked

P1011 topic Link
Here Insert Picture Description

analysis

This question I think for a long time, and finally today have a relatively clear ideas, talk about it.

We first make a table (manually, here to show you use Word to redo a):
Here Insert Picture Description

This title tag has Fibonacci, then what is it associated with the Fibonacci?
We observe forms and understanding the problem can be found, each number is the number of cars on the front and two cars (the number of vehicles on the number of columns similar to the Fibonacci series), is the number of people get off on the last number of the car, which is the relationship .
Not saying that we must set the Fibonacci sequence Fibonacci sequence is investigated ah!

First read the title, there may be some confusion, that the number of second Station this useful? Or will default on a car off a, so you just test data generation to generation will know that they're wrong, went wrong?
In fact, to get off on the second person y, that is another unknown quantity, we should go to war alone to deal with it, to understand this part, you can make your own table above, in order to have the idea of designing algorithms.

Design, I think is relatively simple and crude, open loop iteration values ​​ai solved, and then calculated the coefficient y, minus ai get off with a final m, divided by the coefficient of y to get y, with y, and can be re-run again yi ai again obtained when x, i.e. the sum is the answer.

It is worth mentioning that the last time there is no carry out so-called off last number is actually the last (n-1 station) the remaining number; x-th station but ultimately, if not the last stop, on you can be taken to the site, rather than a stand. This is particularly important, of course, I did not do the first x stop, is not it sentenced last stop, there is no test data, we proposed more detailed some of it!

AC Code (Java description language)

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        //出发人数、站数、抵终人数、待求站号
        int a = scanner.nextInt(), n = scanner.nextInt(), m = scanner.nextInt(), x = scanner.nextInt();
        scanner.close();
        //先算a
        long first_up = a, next_up = 0, sum_a = a, sum_y_num = 0, y = 0, temp_up = 0, temp_down = 0, x_a = a, x_y = 0;
        for (int i = 3; i < n; i++) {
            temp_up = first_up + next_up;
            temp_down = next_up;
            first_up = next_up;
            next_up = temp_up;
            sum_a += (temp_up-temp_down);
        }
        //算y
        first_up = 0;
        next_up = 1;
        for (int i = 3; i < n; i++) {
            temp_up = first_up + next_up;
            temp_down = next_up;
            first_up = next_up;
            next_up = temp_up;
            sum_y_num += (temp_up-temp_down);
        }
        y = (m-sum_a)/sum_y_num;
        first_up = a;
        next_up = 0;
        for (int i = 3; i <= x; i++) {
            temp_up = first_up + next_up;
            temp_down = next_up;
            first_up = next_up;
            next_up = temp_up;
            x_a += (temp_up-temp_down);
        }
        first_up = 0;
        next_up = y;
        for (int i = 3; i <= x; i++) {
            temp_up = first_up + next_up;
            temp_down = next_up;
            first_up = next_up;
            next_up = temp_up;
            x_y += (temp_up-temp_down);
        }
        System.out.println(x_a+x_y);
    }
}

Here Insert Picture Description

Published 358 original articles · won praise 616 · views 30000 +

Guess you like

Origin blog.csdn.net/weixin_43896318/article/details/104079789