1028. Hanoi Tower Sequence
1028. Hanoi Tower Sequence
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
Description
Hanoi Tower is a famous game invented by the French mathematician Edourard Lucas in 1883. We are given a tower of n disks, initially stacked in decreasing size on one of three pegs. The objective is to transfer the entire tower to one of the other pegs, moving only one disk at a time and never moving a larger one onto a smaller.
The best way to tackle this problem is well known: We first transfer the n-1 smallest to a different peg (by recursion), then move the largest, and finally transfer the n-1 smallest back onto the largest. For example, Fig 1 shows the steps of moving 3 disks from peg 1 to peg 3.

Now we can get a sequence which consists of the red numbers of Fig 1: 1, 2, 1, 3, 1, 2, 1. The ith element of the sequence means the label of the disk that is moved in the ith step. When n = 4, we get a longer sequence: 1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1. Obviously, the larger n is, the longer this sequence will be.
Given an integer p, your task is to find out the pth element of this sequence.
Input
The first line of the input file is T, the number of test cases.
Each test case contains one integer p (1<=p<10^100).
Output
Output the pth element of the sequence in a single line. See the sample for the output format.
Print a blank line between the test cases.
Sample Input
4
1
4
100
100000000000000
Sample Output
Case 1: 1 Case 2: 3 Case 3: 3 Case 4: 15
Problem Source
ZSUACM Team Member
// Problem#: 1028
// Submission#: 2346268
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include<iostream>
#include<cmath>
#include<string>
using namespace std; bool diff(string a,string b){
int lena=a.size();
int lenb=b.size();
if(lena<lenb)
return true;
else if(lena == lenb){
int i;
for(i=0;i<lena;i++){
if(a[i]<b[i]){
return true;
}
else if(a[i]==b[i]){
}
else{
return false;
}
}
}
else
return false;
return false;
}
string add(string a,string b){
string temp;
int lena=a.size();
int lenb=b.size();
int x,y;
x=lena;
y=lenb;
if(x>y){
temp='0'+a;
int i=0;
int lent=temp.size();
for(i=1;i<=lenb;i++){
temp[lent-i-1]=((temp[lent-i]-'0')+(b[lenb-i]-'0'))/10+temp[lent-i-1];
temp[lent-i]=((temp[lent-i]-'0')+(b[lenb-i]-'0'))%10+'0'; }
}
else{
temp='0'+b;
int lent=temp.size();
int i=0;
for(i=1;i<=lena;i++){
temp[lent-i-1]=((a[lena-i]-'0')+(temp[lent-i]-'0'))/10+temp[lent-i-1];
temp[lent-i]=((a[lena-i]-'0')+(temp[lent-i]-'0'))%10+'0'; }
}
int lent=temp.size();
int i=0;
while(temp[i]=='0'){
i++;
}
temp.erase(temp.begin(),temp.begin()+i);
return temp;
} string pluss(string a,string b){
string temp;
int lena=a.size();
int lenb=b.size();
int x=lena,y=lenb;
int key;
for(key=1;key<=y;key++){
if(a[lena-key]>=b[lenb-key]){
temp=(char)(a[lena-key]-b[lenb-key]+'0')+temp;
}
else if(a[lena-key-1]!='0'){
a[lena-key-1]=a[lena-key-1]-1;
temp=(char)(a[lena-key]-b[lenb-key]+'0'+10)+temp;
}
else{
int i=1;
while(a[lena-key-i]=='0'){
i++;
}
a[lena-key-i]=a[lena-key-i]-1;
i--;
while(i>=1){
a[lena-key-i]='9';
i--;
}
temp=(char)(a[lena-key]-b[lenb-key]+'0'+10)+temp;
}
}
int lent=temp.size();
int i=0;
while(temp[i]=='0'){
i++;
}
temp.erase(temp.begin(),temp.begin()+i);
return temp;
} int getCase(string p){
int i=0;
string temp;
temp=temp+'1';
string front;
while(diff(temp,p)){
front=temp;
temp=add(temp,temp);
i++;
}
if(temp==p){
return i+1;
}
else{
p=pluss(p,front);
return getCase(p);
}
} int main(){
int n;
cin>>n;
int i=0;
while(n--){
string p;
i++;
cin>>p;
if(n==0){
cout<<"Case "<<i<<": "<<getCase(p)<<endl;
}
else{
cout<<"Case "<<i<<": "<<getCase(p)<<endl<<endl;
}
}
return 0;
}
首先是要找规律,然后是大精度加减法以及比较大小的问题,大精度的那些函数是用string实现的。
至于规律,我们可以简单的写下钱16次操作,我们会发现,当操作次数为2的(0,1,2,3....)次方的时候,对应操作的disk序号一定是第一次出现(1,2,3,4...),而且为操作次数加1,另外你会发现操作数为2的(0,1,2,3....)次方的时候下一位一定从disk1开始重复前面的所有步。
所以我们可以找到离操作数最近的那个2的幂,然后把操作数减去2的幂得到的值赋值给操作数,再用递归的方法求出答案。方法仅供参考。
1028. Hanoi Tower Sequence的更多相关文章
- HDU1329 Hanoi Tower Troubles Again!——S.B.S.
Hanoi Tower Troubles Again! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- ZOJ-1239 Hanoi Tower Troubles Again!
链接:ZOJ1239 Hanoi Tower Troubles Again! Description People stopped moving discs from peg to peg after ...
- Codeforces Gym 100114 A. Hanoi tower 找规律
A. Hanoi tower Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descript ...
- 汉诺塔 Hanoi Tower
电影<猩球崛起>刚开始的时候,年轻的Caesar在玩一种很有意思的游戏,就是汉诺塔...... 汉诺塔源自一个古老的印度传说:在世界的中心贝拿勒斯的圣庙里,一块黄铜板上插着三支宝石针.印度 ...
- HDU 1329 Hanoi Tower Troubles Again!(乱搞)
Hanoi Tower Troubles Again! Problem Description People stopped moving discs from peg to peg after th ...
- 3-6-汉诺塔(Hanoi Tower)问题-栈和队列-第3章-《数据结构》课本源码-严蔚敏吴伟民版
课本源码部分 第3章 栈和队列 - 汉诺塔(Hanoi Tower)问题 ——<数据结构>-严蔚敏.吴伟民版 源码使用说明 链接☛☛☛ <数据结构-C语言版> ...
- Python学习札记(十四) Function4 递归函数 & Hanoi Tower
reference:递归函数 Note 1.在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. eg.计算阶乘: #!/usr/bin/env python3 def ...
- zoj 2954 Hanoi Tower
Hanoi Tower Time Limit: 2 Seconds Memory Limit: 65536 KB You all must know the puzzle named "Th ...
- 10276 - Hanoi Tower Troubles Again!(思维,模拟)
People stopped moving discs from peg to peg after they know the number of steps needed to complete t ...
随机推荐
- scala 稀疏向量
http://mocom.xmu.edu.cn/article/show/58481eb2e083c990247075a5/0/1 1. /创建一个标签为1.0(分类中可视为正样本)的稠密向量标注点 ...
- Java的反射和代理以及注解
最近接触到java的反射和代理(接触的有点迟了...),还是有必要总结下 1. Java的反射 有的时候我们需要在程序运行的时候获取类.方法等信息用于动态运行,这个时候反射就能够帮我们找到类.方法.成 ...
- Python3 bytes 函数
Python3 bytes 函数 Python3 内置函数 描述 bytes 函数返回一个新的 bytes 对象,该对象是一个 0 <= x < 256 区间内的整数不可变序列.它是 b ...
- phpStudy1——PHP文件获取html提交的参数
示例代码: submit.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...
- php SESSON共享 (mysql方式)
为什么要进行session共享? 因为一些大型网站,通常会有很多服务器,每个服务器运行不同的业务模块,并使用二级域名(或是完全不同的域名),而用户系统是统一的,通过登陆名.密码来登陆各模块.用户数据放 ...
- 【.Net姿势随记】const 与 readonly 初始化姿势
using System; class P { static readonly int A=B*10; static readonly int B=10; public ...
- Golang之写一个聊天室
. 海量用户在线聊天系统 . 点对点聊天 . 用户登录&注册 一.服务端开发 . 用户管理 用户id:数字 用户密码:字母数字组合 用户昵称:用来显示 用户性别:字符串 用户头像:url 用户 ...
- 02 请求库之 requests模块
requests模块 一 介绍 #介绍:使用requests可以模拟浏览器的请求,比起之前用到的urllib,requests模块的api更加便捷(本质就是封装了urllib3) #注意:requ ...
- 20172325 2017-2018-2 《Java程序设计》第十周学习总结
20172325 2017-2018-2 <Java程序设计>第十周学习总结 教材学习内容总结 1.集合与数据结构 集合是一种对象 集合按照保存类型来看可以分为两种: (1)同构集合:只能 ...
- SECURITY_ATTRIBUTES 实现最低权限总结
SetSecurityDescriptorDacl函数可以用来设置DACL中的信息.如果一个DACL已经在security descriptor中存在,那么此DACL将被替换.值得注意的是MSDN中的 ...