找规律 递推

设f(n)为需要的步数.

则有递推公式f(n) = 把n -1 个环套上 + 把n -2 个环拿下 + 把n -2 个环套上 + 1
“What are you doing now?”

"Playing Nine Interlinks!"

"What is that?"

"Oh it is an ancient game played over China. The task is to get the nine rings off the stick according to some rules. Now, I have got them off, would you like to have a try to get them on?"

Input

The first line of the input contains an integer T (T <= 30), indicating the number of cases.

Each case consists of a simple integer n (1 < n < 30), which is the number of the total rings you need to get on the stick.

At the beginning, all rings are off the stick.

In each step, you can only get one ring on or off by the following rules:

1. You can get the first ring on or off freely at each step.

2. If the ith ring is on the stick, and the 1st, 2nd... (i-1)st rings are off the stick, you can get the (i+1)st ring on or off freely at each step.

Output

For each case, print in a single line the minimum number of steps you need to get n rings on the stick.

Sample Input

2
2
3

Sample Output

2
5

Hint
The first sample: 1 on, 2 on.
The second sample: 1 on, 2 on, 1 off, 3 on, 1 on. 
#include<iostream>
using namespace std;
const	int N=32;
int f[N];
int main()
{
	int T;
	f[1]=1,f[2]=2;
	for(int i=3;i<=30;i++)
	f[i]=2*f[i-2]+f[i-1]+1;
//	cout<<f[30]<<endl; 
	cin>>T;
	while(T--)
	{
		int n;
		cin>>n;
		cout<<f[n]<<endl;	
	}
}
全部评论

相关推荐

10-17 10:05
已编辑
北华大学 全栈开发
牛客872465272号:掉头发了哥
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务