P3612
每次倍增+大数,暗示使用折半。
这个题目自己想到了,但是如下两点要注意:
注意两点:
- 字符串地址-1
- 轮转过程是要调头的(思考,如果刚好在半处,不该停在这一头,应该找找大端呢)
知识总结:
一般RE归结为2-3类:
- 暴栈:检查递归的处理,检查函数里有没有开大数组
- 越界:仔细读代码的逻辑
- /0 或者%0 :自己细读自己的代码
代码
使用位运算进行折半看起来是一个更好的选择。
#include <iostream>
using namespace std;
long long base, tmp;
string s;
int main()
{
cin >> s;
int l = s.size();
cin >> tmp;
tmp -= 1;
base = l;
while (base <= tmp)
base <<= 1;
base >>= 1;
while (tmp > l)
{
if (tmp >= base)
{
tmp -= base;
if (tmp == 0)
tmp = base - 1;
else tmp -= 1;
}
base >>= 1;
}
cout << s[tmp] << endl;
return 0;
}