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;}