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 基本语法
1.对于一个二元数组,键值对(k,v) 用map的时候,要取第一个或者第二个元素,用 _1 或者 _2表示. scala> words.take(2)res40: Array[(String, ...
- sparkSQL、dataframe
http://www.aboutyun.com/forum.php?mod=viewthread&tid=12358&page=1 空值填充:http://spark.apache.o ...
- Wilcoxon Signed Rank Test
1.Wilcoxon Signed Rank Test Wilcoxon有符号秩检验(也称为Wilcoxon有符号秩和检验)是一种非参数检验.当统计数据中使用“非参数”一词时,并不意味着您对总体一无所 ...
- Analyzing Microarray Data with R
1) 熟悉CEL file 从 NCBI GEO (http://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=GSE24460)下载GSE24460. 将得到 ...
- ReactNative手势解锁(react-native-ok-gesture-password)
在大前端的趋势之下,我也慢慢开始从事React Native相关的开发.但是奈何React Native生态相对于Android来说还是太小了.许多开源的库早早就已经不再维护.之前项目中需要用到手势解 ...
- python3 回顾笔记1
http://www.runoob.com/python3/python3-tutorial.html这个网址,可以学习python3的基础语法. 1. 单引号和双引号意义完全相同.用r可以限制转义符 ...
- TZOJ 1840 Jack Straws(线段相交+并查集)
描述 In the game of Jack Straws, a number of plastic or wooden "straws" are dumped on the ta ...
- The valid characters are defined in RFC 7230 and RFC 3986
网上大都说什么发送格式与协议定义的不兼容,改tomcat版本或改编码之类的. 本人测试的时候换了个浏览器,不用IE就好了 如果坚持用ie,也有解决方式 @参考文章 成功的方法 在请求地址var url ...
- php代码执行顺序
从上往下,调用类里面的方法,类放上面,调用在下面
- Nginx详解(正向代理、反向代理、负载均衡原理)
Nginx配置详解 nginx概述 nginx是一款自由的.开源的.高性能的HTTP服务器和反向代理服务器:同时也是一个IMAP.POP3.SMTP代理服务器:nginx可以作为一个HTTP服务器进行 ...