程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> ZOJ 1633 Big String

ZOJ 1633 Big String

編輯:C++入門知識

ZOJ 1633 Big String


Big String

Time Limit: 2 Seconds Memory Limit: 65536 KB

We will construct an infinitely long string from two short strings: A = "^__^" (four characters), and B = "T.T" (three characters). Repeat the following steps:
    Concatenate A after B to obtain a new string C. For example, if A = "^__^" and B = "T.T", then C = BA = "T.T^__^".Let A = B, B = C -- as the example above A = "T.T", B = "T.T^__^". Your task is to find out the n-th character of this infinite string.


    Input

    The input contains multiple test cases, each contains only one integer N (1 <= N <= 2^63 - 1). Proceed to the end of file.


    Output

    For each test case, print one character on each line, which is the N-th (index begins with 1) character of this infinite string.


    Sample Input

    1
    2
    4
    8
    


    Sample Output

    T
    .
    ^
    T
    
    
    題意:開始時有兩個字符串,A = "^__^" (four characters),  B = "T.T" (three characters),然後重復執行C=BA,A=B,B = C,問第n個字符是什麼。
    
    
    分析:因為最終的字符串是從前往後遞推出來的,所以再求解時,我們可以把這個過程逆過去,直到字符串的長度不超過7,輸出即可。
    
    
    #include
    #include
    using namespace std;
    typedef long long LL;
    LL a[95];  //保存字符串的長度
    int main()
    {
        string C = "T.T^__^";
        a[0] = 4;
        a[1] = 3;
        int i;
        for(i = 2; i < 90; i++)
            a[i] = a[i-1] + a[i-2];
        LL n;
        while(cin >> n)
        {
            while(n > 7)
            {
                int pos = lower_bound(a, a+89, n) - a;
                n -= a[pos-1];
            }
            cout << C[n-1] << endl;
        }
        return 0;
    }


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved