题目链接:这里哟
2020 排序相减
题目来源: syu练习题
基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注
“排序相减”操作是指对于任意一个四位数n,将四个数字分别进行顺序排序和逆序排序,得到两个数取相减后结果的绝对值n1,然后继续将n1中的四个数字进行顺序排序和逆序排序,得到两个数取相减后结果的绝对值n2,以此类推,最后总会得到一个数字黑洞,无法跳出。
例如:样例2中4176 = 6532 - 2356
Input
第一行输入一个整数T,表示数据组数(1
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
int a[4];
int t;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
int ans=n;
while(m--)
{
int j=0;
while(ans) //循环得到每一位上的数字哟
{
a[j++]=ans%10;
ans/=10;
}
sort(a,a+4);
int ant1=a[0]+a[1]*10+a[2]*100+a[3]*1000;
int ant2=a[0]*1000+a[1]*100+a[2]*10+a[3];
ans=ant1-ant2;
}
printf("%d\n",ans);
}
return 0;
}