C language-mathematics-statistical problem (Hdu 2563)

Source: Internet
Author: User

C language-mathematics-statistical problem (Hdu 2563)
Problem Description in an infinitely large two-dimensional plane, we make the following assumptions:
1. Only one cell can be moved at a time;
2. You cannot go backward (If your destination is "up", you can go left, right, or up, but not down );
3. The passing grid collapsed immediately and could not go for the second time;

Calculate the number of different solutions in n steps (two steps are considered different if one step is different ).

Input first gives a positive integer C, indicating that there are group C Test Data
In row C, each row contains an integer n (n <= 20), which indicates that n steps are required.

For the Output, Please program the total number of different solutions that take n steps;
The output of each group occupies one row.

Sample Input

212


Here we always define the forward direction.
We use A [I]Indicates the type of the forward step. We use B [I]Indicates the type of step I to the left (right ). A [1] = 3, B [1] = 2;
Then the idea of sub-Governance: Moving Forward I step (I> 1) is equivalent to moving forward the type of the I-1 step plus to the left, to the right to take the type of the I-1 step. The mathematical expression is: A [I] = a [I-1] + 2 * B [I-1];The type of step I (I> 1) to the left (to the right) is equivalent to the type of the forward I-1 step plus the type of the right (to the left) to the I-1 step: B [I] = a [I-1] + B [I-1];
Then, it is obtained by recursion.

The Code is as follows:
#include 
 
  int main(){    int a[21]={0,3},b[21]={0,2},i;    for(i=2;i<21;i++)    {        b[i]=a[i-1]+b[i-1];        a[i]=a[i-1]+2*b[i-1];    }    int m,N;    scanf("%d",&N);    while(N--)    {        scanf("%d",&m);        printf("%d\n",a[m]);    }    return 0;}
 




Related Article

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.