ABC192 C - Kaprekar Number(字符串和数字的相互转换)

题意:

在这里插入图片描述

解法:

直接模拟即可,主要难点在于字符串和数字之间的相互转换.

数字转字符串to_sting()
字符串转数字itos()

code:

#include<bits/stdc++.h>
using namespace std;
const int maxm=2e6+5;
bool cmp(char a,char b){
    
    
    return a>b;
}
bool cmp2(char a,char b){
    
    
    return a<b;
}
void solve(){
    
    
    int n,k;cin>>n>>k ;
    int now=n;
    for(int i=1;i<=k;i++){
    
    
        string s=to_string(now);
        string ss=to_string(now);
        sort(s.begin(),s.end(),cmp);
        sort(ss.begin(),ss.end(),cmp2);
        int x=stoi(s);
        int y=stoi(ss);
        now=x-y;
    }
    cout<<now<<endl;
}
signed main(){
    
    
    solve();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44178736/article/details/115174308