sicily 1028. Hanoi Tower Sequence

原创 2015年11月17日 16:07:19

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

题目分析

大整数除法

计算给定的大整数能整除2的次数加1


#include <iostream>
#include <string.h>

int a[101];
int len;

int find(int start) {
  if (a[len - 1] % 2 == 1)
    return 0;
    
  for (int d = start; d < len - 1; ++d) {
    a[d + 1] += (a[d] % 2) * 10;
    a[d] /= 2;    
  }
  a[len - 1] /= 2;
  
  if (a[start] == 0)
    start++;
  return 1 + find(start);  
}

int main()
{
  int test;
  std::cin >> test;
  std::string key; 
   
  for (int c = 1; c <= test; ++c) {
    if (c != 1)
      std::cout << std::endl;  

    std::cin >> key;
    memset(a, 0, sizeof(a));
    for (int d = 0; key[d] != '\0'; ++d)
      a[d] = key[d] - '0';
    len = key.length();
    
    std::cout << "Case " << c << ": " << 1 + find(0) << std::endl;    
  }  
  return 0;
}




版权声明:本文为博主原创文章,未经博主允许不得转载。

sicily 1028. Hanoi Tower Sequence

1028. Hanoi Tower Sequence Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Ha...

Sicily 1028 Hanoi Tower Sequence

汉诺塔问题,最经典的解法是递归,但是这道题的数据太大,没有办法实现。实际上这道题要求的是找出某一位置的汉诺块,没有必要完全模拟,而是要找出特定的汉诺塔会出现在哪些位置。 观察移动的汉诺塔块的序列,可...
  • wbchou
  • wbchou
  • 2013年02月17日 21:17
  • 482

<OJ_Sicily>Hanoi_Tower_Sequence

汉诺塔

汉诺塔问题 hanoi tower (递归)。

又来填坑了。 还是递归问题,这次是汉诺塔。 题意是这样的:有三根相邻的柱子,标号为A,B,C,A柱子上从下到上按金字塔状叠放着n个不同大小的圆盘,要把所有盘子一个一个移动到柱子C上,并且每次移动同...

Hanoi Tower 汉诺塔的简单分析/C

当然、这是一个经典的递归问题~     想必来看这篇博文的同学对汉诺塔应该不会陌生了吧,   写这篇博还是有初衷的:   之前学数据结构的时候自己看书、也上网上查了很多资料,资料都比较散、而且描...

Tower of Hanoi

汉诺塔其实也就这么回事         初涉汉诺塔相关题目是在大一上学期C语言课递归章节,当时递归一知半解,汉诺塔一窍不通。经过了一年多的风霜洗礼,最近又见到汉诺塔的题,理解起来就容易多了。     ...

HANOI TOWER recursion implementation & parse - 汉诺塔

#include #include /* Name:HANOI TOWER Copyright: Author:yilonglucky Date: 01-08-1...

【进阶的汉诺塔】code forces 392B Tower of Hanoi

汉诺塔变形 用到了记忆化搜索、递归……

河内塔问题(Hanoi Tower)

河内塔 /** * 河内塔问题求解(递归:中序遍历二叉树) * * @param level 缩进级别:用于控制输出的“缩进”量,纯美观设计,初学者可忽略它; * 递归树的根缩进量为0...

Hanoi Tower 汉诺塔的简单分析/C

有一个梵塔,塔内有三个座A、B、C,A座上有诺干个盘子,盘子大小不等,大的在下,小的在上(如图)。把这些个盘子从A座移到C座,中间可以借用B座但每次只能允许移动一个盘子,并且在移动过程中,3个座上的盘...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:sicily 1028. Hanoi Tower Sequence
举报原因:
原因补充:

(最多只允许输入30个字)