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^__^".
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 = ^ __ ^(四个字符)和B = "T.T”(三个字符)。
重复以下步骤:
连接后B获得一个新的字符串c。
例如,如果一个= B =“^ __ ^ T.T”,那么C =BA= " T.T ^ __ ^”。
让A = B, B = C就像上面的例子。A = "T.T", B = "T.T^__^".
你的任务是找出无限第n个字符的字符串。
解题思路:
不要被题目迷惑,就是个简单的斐波拉契题。
虽然题目的字符串叠加后有一定规律,但是还是感觉和输入的数据n没有直接关系,但是如果从字符串长度找规律还是能发现的。
a=4, b=3, c=7
字符串长度就是个斐波拉契数列
AC:
#include <cstdio>
#include <iostream>
#include <string>
#define LEN 88
using namespace std;
int main()
{
string base="T.T^__^";
long long int f[LEN];
f[0]=7;
f[1]=10;
for(int i=2; i<LEN; i++)
f[i] = f[i-1]+f[i-2];
long long int n;
while(cin>>n)
{
while(n>7)
{
int i=0;
while(i<LEN && f[i]<n)
i++;
n-=f[i-1];
}
cout<<base[n-1]<<endl;
}
return 0;
}