跳到正文

题解

题解:P11825 [TOIP2024] 6174

#SUSPENDED#约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟
题目
P11825

版本

  1. v1当前

思路

定义两个函数分别将 sis_i 的数位升序、降序排列,然后依题意模拟即可,每一步得到的数存放在一个 unordered_map 里,当遇到某一个数出现 22 次时就输出这个数并终止循环。

代码

C++
#include<bits/stdc++.h>
using namespace std;
bool cmp(long long a,long long b){
	return a>b;
}
int t,k;
long long asc(long long x){
	long long a[11];
	for(int i=1;i<=k;i++){
		a[i]=x%10;
		x/=10;
	}
	sort(a+1,a+k+1);
	x=0;
	for(int i=1;i<=k;i++){
		x=x*10+a[i];
	}
	return x;
}
long long desc(long long x){
	long long a[11];
	for(int i=1;i<=k;i++){
		a[i]=x%10;
		x/=10;
	}
	sort(a+1,a+k+1,cmp);
	x=0;
	for(int i=1;i<=k;i++){
		x=x*10+a[i];
	}
	return x;
}
unordered_map<long long,int>s;
bool flag;
int main(){
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	cin>>t>>k;
	while(t--){
		long long n;
		cin>>n;
		int a=0;
		flag=false;
		s.clear();
		s[n]++;
		for(int i=1;i<=100000;i++){
			n=desc(n)-asc(n);
			s[n]++;
			if(s[n]>1){
				cout<<n<<endl;
				break;
			}
		}
	}
}
原文