logo AlgoBeat OnlineJudge
登录 注册

题解

作者: tyxx040105  ·  发布于 2026-09-11 14:20:41  ·  最后修改于 2026-09-12 11:25:30
已通过
审核员:Carey_chen HCl · 2026-09-12 11:25:30

使用string中的reverse函数,注意要开long long。

#include<bits/stdc++.h>
using namespace std;
int main(){
    long long n,k,sum=0;
    cin>>n>>k;
    for(int i=0;i<n;i++){
        long long a;
        cin>>a;
        sum+=a;
    }
    string s=to_string(sum),bs=s;
    int siz=s.size();
    for(int i=0;i<siz;i++){
        for(int j=i;j<siz;j++){
            if(j-i+1>k) break;
            string tmp=s;
            reverse(tmp.begin()+i,tmp.begin()+j+1);
            if(tmp>bs) bs=tmp;
        }
    }
    long long ans=stoll(bs);
    cout<<ans;
    return 0;
}

暂无评论

登录 后即可评论。