Sicily 1028. Hanoi Tower Sequence

来源:互联网 发布:系统工程师 软件开发 编辑:程序博客网 时间:2024/05/21 21:02

国庆刷一刷。汉诺塔序列,其实就是找规律,初始化ans为1。如果数字为奇数,直接输出ans,若为偶数则除2,直到数字为奇数,每除一次ans加1。最后的答案就是ans。

// Problem#: 1028// Submission#: 2249832// 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 <vector>#include <string.h>#include <algorithm>#include <cstdio>#include <cmath>#include <queue>#include <map>#include <set>#include <string>#include <stack>#include <bitset>using namespace std;int main(){    //freopen("in.txt", "r", stdin);    int t;    cin >> t;    for(int k = 1; k <= t; k++){        if(k != 1)  cout <<endl;        string s;        cin >> s;        if((s[s.size()-1]-'0') % 2 != 0){            cout <<"Case "<< k << ": "<< 1 <<endl;            continue;        }        int cont = 0;        while(!s.empty()){            string tmp = s;            s.clear();            int ans = 0, div;            for(int i = 0; i < tmp.size(); i++){                ans = ans * 10 + (tmp[i]-'0');                div = ans / 2;                ans %= 2;                if(i == 0 && div == 0)  continue;                s += char(div+'0');                //cout << div <<endl;            }            //cout << s <<endl;            if(ans == 1){                cout <<"Case "<< k << ": "<< cont+1 << endl;                break;            }            cont++;        }    }}                                 


原创粉丝点击