11.17 Homework Problem A: Rabbit breeding problem __ Hkust OJ

Source: Internet
Author: User

Description

Suppose a pair of rabbits can produce a pair of rabbits a month (a female and a male), the next month after the birth of each rabbit is not reproductive ability, the third month after birth can also be a monthly birth of a team of rabbits, asked from a pair of newborn rabbit began, after several months after a total number of rabbits (assuming the rabbit did not die in the process).

The problem is the Italian mathematician Fibonacci (Fibonacci) in his 1202 edition of the Abacus book, the number of rabbits from the first pair of newborn rabbits started every month by the Fibonacci sequence. Input

The first number you enter is N, and then there are n numbers. Each number is one month m (m<=45). Output

The output is n rows, the total number of rabbits after each act m months. Sample Input 6 1 2 3 4 5 Sample Output 1 2 3 5 8 HINT

It's time to timeout.

Is it appropriate to use recursion to compute the Fibonacci sequence?

It is convenient to write with an array, and it is not timed out with a loop.

idea:

it's not appropriate to use the Fibonacci sequence. Tile defy results with Fibonacci input 45 and so on 10s to output a large number of strings. So the array is really strong invincible.

I'll take it. In fact, the 45 will exceed the bounds without LLD. But the OJ seems to be the same with Int.

There's also a bad habit of int arrays in a for loop. Don't learn.

Code:

#include <stdio.h>
#include <string.h>
int main ()
{
    int i, n, S, C;
    scanf ("%d", &s);
    for (c = 1; C <= S; C + +)
    {
    scanf ("%d", &n);
    Long long  int a[47] = {1,2};
    for (i = 2; I <= n; i++)
    {

        A[i] = a[i-1] + a[i-2];
    printf ("%lld\n", a[n-1]);
    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.