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的更多相关文章

  1. 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 ...

  2. ZOJ-1239 Hanoi Tower Troubles Again!

    链接:ZOJ1239 Hanoi Tower Troubles Again! Description People stopped moving discs from peg to peg after ...

  3. Codeforces Gym 100114 A. Hanoi tower 找规律

    A. Hanoi tower Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descript ...

  4. 汉诺塔 Hanoi Tower

    电影<猩球崛起>刚开始的时候,年轻的Caesar在玩一种很有意思的游戏,就是汉诺塔...... 汉诺塔源自一个古老的印度传说:在世界的中心贝拿勒斯的圣庙里,一块黄铜板上插着三支宝石针.印度 ...

  5. HDU 1329 Hanoi Tower Troubles Again!(乱搞)

    Hanoi Tower Troubles Again! Problem Description People stopped moving discs from peg to peg after th ...

  6. 3-6-汉诺塔(Hanoi Tower)问题-栈和队列-第3章-《数据结构》课本源码-严蔚敏吴伟民版

    课本源码部分 第3章  栈和队列 - 汉诺塔(Hanoi Tower)问题 ——<数据结构>-严蔚敏.吴伟民版        源码使用说明  链接☛☛☛ <数据结构-C语言版> ...

  7. Python学习札记(十四) Function4 递归函数 & Hanoi Tower

    reference:递归函数 Note 1.在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. eg.计算阶乘: #!/usr/bin/env python3 def ...

  8. zoj 2954 Hanoi Tower

    Hanoi Tower Time Limit: 2 Seconds Memory Limit: 65536 KB You all must know the puzzle named "Th ...

  9. 10276 - Hanoi Tower Troubles Again!(思维,模拟)

    People stopped moving discs from peg to peg after they know the number of steps needed to complete t ...

随机推荐

  1. servlet类第二篇

    1servlet的生命周期是什么? 服务器启动时(web.xml中配置load-on-startup=1,默认为0)或者第一次请求该servlet时,就会初始化一个Servlet对象,也就是会执行初始 ...

  2. 奇偶数判断1(if,else if语句)

    public class 奇偶数判断 { public static void main(String [] args){ float s = 9f; //取单浮点型变量s,可为任意值 float h ...

  3. ubuntu18 realsenseD435i

    (flappbird) luo@luo-All-Series:~/librealsense/build201901/tools/enumerate-devices$ ./rs-enumerate-de ...

  4. [leetcode]139. Word Break单词能否拆分

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

  5. 四种强制类型转换的总结(const_cast、static_cast、dynamic_cast、reinterpreter_cast)

    四种强制类型转换的总结(const_cast.static_cast.dynamic_cast.reinterpreter_cast) 转载 2011年10月03日 23:59:05 标签: stru ...

  6. struts2框架值栈的概述之问题一:什么是值栈?

    1. 问题一:什么是值栈? * 值栈就相当于Struts2框架的数据的中转站,向值栈存入一些数据.从值栈中获取到数据. * ValueStack 是 struts2 提供一个接口,实现类 OgnlVa ...

  7. Spring框架整合WEB解决配置文件加载多次的问题

    1. 创建JavaWEB项目,引入Spring的开发包.编写具体的类和方法. * 环境搭建好后,启动服务器来测试项目,发送每访问一次都会加载一次配置文件,这样效率会非常非常慢!! 2. 解决上面的问题 ...

  8. js取当前页面名称

    // 取当前页面名称(不带后缀名)    function pageName()    {        var a = location.href;        var b = a.split(& ...

  9. c语言使用指针实现模拟java/c# string.concat字符串串联方法

    #include <stdio.h> void _strcat(char *, const char *); int main(void) { char source[] ="V ...

  10. HDFS高可用性及其分布式系统思想基础

    源自单点失效问题,也就是当NameNode不可用的时候,用什么办法可以平滑过渡? 最直接的办法是再添加一个备用的NN,这就产生了Active NameNode和Standby NameNode的设计思 ...