[sicily online]1028. Hanoi Tower Sequence

來源:互聯網
上載者:User
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

414100100000000000000

Sample Output

Case 1: 1 Case 2: 3 Case 3: 3 Case 4: 15

題目分析:

1,大整數,肯定用string類型來表示

2,看例子:

1:1

2:121

3:1213121

4:121312141213121

可以發現規律第n組是(n-1的資料)+n+(n-1組的資料)

還有一個規律是數列121312141213121,其中,1,2,3的出現都是等間隔的,若要求n  n%2=1的話 結果就是1 n%4=2結果就是2 n%8=3 結果就是3

可以看出n一直除以2 除的次數+1 就是結果

#include<iostream>#include<stdio.h>#include<cmath>#include<iomanip>#include <vector>#include <string>#include <algorithm>#include <sstream>using namespace std;//傳回值為string為除數  int為餘數pair<string,int> strMod(string x1,int x2){int num=0;string result;for(string::size_type i=0;i<x1.size();i++){num=num*10+(x1[i]-'0');int tmp=num/2;num=num%x2;result=result+(char)(tmp+'0');}while(1){if(result.size()==1)break;if(result[0]=='0')result.erase(result.begin());else break;}return make_pair(result,num);}int main(){int n;cin>>n;for(int i=0;i<n;i++){string data;cin>>data;cout<<"Case "<<i+1<<": ";int count=0;for(string::size_type j=0;;j++){pair<string ,int> tmp=strMod(data,2);data=tmp.first;if(tmp.second==0){count++;}else break;}cout<<count+1<<endl;if(i!=n-1)cout<<endl;}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.