G题

Digital Folding

https://ac.nowcoder.com/acm/contest/120561/G

G题(参考大佬的思路,代码建议去看大佬的)
由于分类讨论种类太多,容易出错,可以把数字转化为字符串,然后逐位减1,后面的全变为9,跟l比较大小,符合条件的再翻转字符串更新最大值
#include <bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
        long long l,r,ans=0;
        cin>>l>>r;
        string s1=to_string(l);
        string s2=to_string(r);
        string s=s2;
        reverse(s.begin(),s.end());
        ans=stoll(s);
        for(int i=1;i<=s2.size();i++){
            if((s2[i-1]-'0')>0){
                string s3=s2;
                s3[i-1]--;
                for(int j=i+1;j<=s2.size();j++)
                    s3[j-1]='9';
                long long m=stoll(s3);
                if(m>=l){
                    string S=to_string(m);
                    reverse(S.begin(),S.end());
                    long long n=stoll(S);
                    ans=max(ans,n);
                }
            }
        }
        cout<<ans<<endl;
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务