关闭

sicily 1028. Hanoi Tower Sequence

164人阅读 评论(0) 收藏 举报
分类:

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;
}




0
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:23447次
    • 积分:1241
    • 等级:
    • 排名:千里之外
    • 原创:104篇
    • 转载:0篇
    • 译文:1篇
    • 评论:3条
    文章分类
    最新评论