题解 CF999B 【Reversing Encryption】
代码很简单,逆推其实也很简单,和加密方法一样,其实也是一样的,速度还是挺快的,不知怎么回事cin240ms排第三,快读竟然变成272ms……:
快读代码:
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
inline int read()
{
int x = 0;
char ch = getchar();
while(ch >= '0' && ch <= '9')
{
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return x;
}
string s;
int main()
{
int n;
n = read();
cin >> s;
for(int i = 2; i <= n; i++)
{
if(n % i == 0)
{
reverse(s.begin(), s.begin() + i);
}
}
cout << s << endl;
return 0;
}
cin代码:
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
bool yinshu(int x, int n)
{
return n % x == 0;
}
string s;
int main()
{
int n;
cin >> n >> s;
for(int i = 2; i <= n; i++)
{
if(yinshu(i, n))
{
reverse(s.begin(), s.begin() + i);
}
}
cout << s << endl;
return 0;
}
还有cin不用函数的,242ms:
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
bool yinshu(int x, int n)
{
return n % x == 0;
}
string s;
int main()
{
int n;
cin >> n >> s;
for(int i = 2; i <= n; i++)
{
if(n % i == 0)
{
reverse(s.begin(), s.begin() + i);
}
}
cout << s << endl;
return 0;
}
reverse针对数组和STL容器,两个值均为迭代器,而STL中可以迭代器 + int类型的数据只有vector,string。
注意,迭代器名++不要写成 迭代器名 = 迭代器名 + 1,迭代器的++,--,!=这些都是系统重载过的,和普通理解的不一样
注意循环从2开始就好了,为什么题目也说了

浙公网安备 33010602011771号