测试用例:
第一行 q
第二行 字符串 (需要保证为2的n次方,且n大于q)
如:
输入:
1
ABCD
输出:
BA DC
输入:
2
ABCDEFGH
输出:
DCBA HGFE
暴力解法:
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;
int main(){
int q;
cin >> q;
string str;
cin >> str;
int len = str.size(); //获取输入字符串长度,即为2^n
string tmp;
int cnt = 1;
int k = 0;
for(int i = 0; i < len; i++){
tmp.push_back(str[i]);
k++;
if(k == pow(2, q)){
reverse(tmp.begin(), tmp.end());//翻转字符数组
cout << tmp << " "; //输出翻转字符数组
tmp.erase(); //字符数组清空
string tmp; //重新声明一个字符数组
k = 0;
}
}
cout << tmp << endl;
system("pause");
return 0;
}