51nod 2020

“排序相减”操作是指对于任意一个四位数n,将四个数字分别进行顺序排序和逆序排序,得到两个数取相减后结果的绝对值n1,然后继续将n1中的四个数字进行顺序排序和逆序排序,得到两个数取相减后结果的绝对值n2,以此类推,最后总会得到一个数字黑洞,无法跳出。

 

例如:样例2中4176 = 6532 - 2356

#include <iostream>
#include <algorithm>
#include <cstdio>
bool cmp(int a,int b)
{
    return a>b;
}
using namespace std;
int fun(int s)
{
    int i;
    int num,m,n;
    int a[5],b[5];
    num=s;
    i=0;
    while(num)
    {
        a[i]=b[i]=num%10;
        num/=10;
        i++;

    }
    sort(a,a+4);
    sort(b,b+4,cmp);
    m=a[0]*1000+a[1]*100+a[2]*10+a[3];
    n=b[0]*1000+b[1]*100+b[2]*10+b[3];
    return n-m;
}
int main()
{
    int t,n,num;
    cin>>t;
    while(t--)
    {
        cin>>num>>n;
        int x;
        x=num;
        while(n--)

        {
            x=fun(x);
        }
        cout<<x<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值