[HDU] [2041] Super Stairs

Problem Description

Loading...

Problem Analysis

To reach stair n there are only two cases: from stair n-2 or from stair n-1
So we have
a[i]=a[i-1]+a[i-2]
the Fibonacci sequence

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#include<stdio.h>
int main() {
	int a[50];
	for(int i=1; i<=45; i++) {
		if(i==1||i==2)
			a[i]=1;
		else
			a[i]=a[i-1]+a[i-2];
	}
	int T;
	scanf("%d",&T);
	while(T--) {
		int n;
		scanf("%d",&n);
		printf("%d\n",a[n]);
	}
	return 0;
}

Problem link: [HDU] [2041] Super Stairs

Loading...