九连环问题

  九连环是我国传统的民间智力玩具,玩具上面有九个连环套在杆上,目标就是通过一定的方式将九个连环从杆上全部取下来。
  玩法是这样的:
1、对每个环,有2种操作:把这个环放到杆上或把这个环从杆上取下
2、你可以随意的对第1个环进行操作
3、如果你想对第i个环(i>1)进行操作,你必须将第i-1个环放在杆上,且必须把前i-2个环从杆上取下
九连环问题:
  有两种题型,题型一:就是简单的让你输出取下第n个环的最少步骤数;题型二:让你写出操作具体过程

题型一:ZOJ - 3182
  “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.
找规律:

 n  step
 0  0
 1  1
 2  2( = 0 + 20 )
 3  5( = 1 + 22 )
 4  10( = 2 + 23)
 5  21( = 5 + 24)
 6  42( = 10 + 25 )
 m  s( = a[i-2] + pow(2,i-1) )
#include<iostream>
#include<cmath>
#include<algorithm>

using namespace std;

int main(void)
{
	int T,n;
	int a[44]={0,1,2};
	for(int i=3;i<=32;i++){
        a[i] = a[i-2] + (int)pow(2,i-1);
	}
	cin>>T;
	while(T--){
        cin>>n;
        cout<<a[n]<<endl;
    }
	return 0;
}

题型二:“华为杯”山东理工大学第十一届ACM程序设计竞赛(正式赛)网络同步赛
如果你不了解九连环,那玄黄就带你领略九连环的奥妙:
  九连环是我国传统的民间智力玩具,玩具上面有九个连环套在杆上,目标就是通过一定的方式将九个连环从杆上全部取下来。
玩法是这样的:
  1、对每个环,有2种操作:把这个环放到杆上或把这个环从杆上取下
  2、你可以随意的对第1个环进行操作
  3、如果你想对第i个环(i>1)进行操作,你必须将第i-1个环放在杆上,且必须把前i-2个环从杆上取下
Input
  输入一个整数n ( 0<n<10 ),代表杆上面的连环个数。
Output
  输出把所有连环取下来的最少操作步骤,每一步占一行,输出一个整数i和操作”UP”或者”DOWN”,代表将第i个环放到杆上或从杆上取下来,整数和操作用空格分开。详情见示例输出。
Sample Input
4
Sample Output
2 DOWN
1 DOWN
4 DOWN
1 UP
2 UP
1 DOWN
3 DOWN
1 UP
2 DOWN
1 DOWN
递归思路解决:
模拟下环、上环的过程:
以六个环(C1~C6)为例:
(1)想拿下C6,就要先拿下C4,而想拿下C2
具体过程:拿下C2,拿下C1,拿下C4,放上C1,放上C2,拿下C1,拿下C3,放上C1,拿下C2,拿下C1,拿下C6;
拿上C1,拿上C2,放下C1,拿上C3,拿上C1,放下C2,放下C1,拿上C4,
…………

#include<cstdio>
#include<iostream>
using namespace std;
void shanghuan(int n);
void xiehuan(int n){
    if(n>2) xiehuan(n-2);
    printf("%d DOWN\n",n);
    if(n>2) shanghuan(n-2);
    if(n>1) xiehuan(n-1);
}
void shanghuan(int n){
    if(n>1) shanghuan(n-1);
    if(n>2) xiehuan(n-2);
    printf("%d UP\n",n);
    if(n>2) shanghuan(n-2);
}
int main(void)
{
	int n;
	cin>>n;
	xiehuan(n);
	return 0;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逃夭丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值