解题思路
我们可以建立一个桶,存放你的数有没出现。如果我们减出来的数你出现过1,第一个发现的即是答案,立马输出,退出循环。
当然,这个数可以达到 位数,如果我们用建立一个 的 bool 类型桶,直接就 了。我们需要建立 map 表示桶。
需要知道这几个函数2:
to_string(整数):把整数类型3转换为string类型。stoll(str):把string类型转换为long long类型。如果要转换int类型,请使用stoi(str)。
要注意
输入的数没有前导 ,需要手动补上。(WA 记录)
AC 代码4:
#include<iostream>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
map<long long,bool> t;
int main(){
int n,d;
cin>>n>>d;
for(int i=1;i<=n;i++){
t.clear();//记得清空桶哦!
long long x;
cin>>x;
while(!t.count(x)){
t[x]=1;
string a=to_string(x);
if(a.size()!=d){
int cha=d-a.size();
for(int i=1;i<=cha;i++){
a='0'+a;
}
}
string b=a;
sort(a.begin(),a.end(),greater<char>());
sort(b.begin(),b.end());
x=(stoll(a)-stoll(b));
}
cout<<x<<'\n';
}
return 0;
}