题解 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开始就好了,为什么题目也说了

posted @   HappyBobb  阅读(3)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示