找规律 递推

设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;	
	}
}
全部评论

相关推荐

明天不下雨了:让我们大声的说出来:以前的未来就是现在
点赞 评论 收藏
分享
05-09 14:45
门头沟学院 Java
点赞 评论 收藏
分享
06-17 00:26
门头沟学院 Java
程序员小白条:建议换下项目,智能 AI 旅游推荐平台:https://github.com/luoye6/vue3_tourism_frontend 智能 AI 校园二手交易平台:https://github.com/luoye6/vue3_trade_frontend GPT 智能图书馆:https://github.com/luoye6/Vue_BookManageSystem 选项目要选自己能掌握的,然后最好能自己拓展的,分布式这种尽量别去写,不然你只能背八股文了,另外实习的话要多投,尤其是学历不利的情况下,多找几段实习,最好公司title大一点的
无实习如何秋招上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务