zoukankan      html  css  js  c++  java
  • PAT:1069. The Black Hole of Numbers (20) AC

    #include<stdio.h>
    #include<algorithm>
    using namespace std;
    const int AIM=6174;
    
    int n;
    int arr[4];
    
    bool NonIncreasingOrder(int a,int b)
    {
      return a>b;
    }
    
    bool NonDecreasingOrder(int a,int b)
    {
      return a<b;
    }
    
    int toNum()          //得到这个数字
    {
      int sum=0;
      for(int i=0 ; i<4 ; ++i)
      {
        sum=sum*10+arr[i];
      }
      return sum;
    }
    
    void toArr()
    {
      for(int i=0 ; i<4 ; ++i)    //拆解数字
      {
        arr[i]=n%10;
        n/=10;
      }
    }
    int main()
    {
    
      scanf("%d",&n);
      int MIN,MAX;            //最小排列数,最大排列数
      
      while(1)
      {
        toArr();                //拆解
        sort(arr,arr+4,NonIncreasingOrder);    //生成最大数
        MAX=toNum();
        sort(arr,arr+4,NonDecreasingOrder);    //生成最小数
        MIN=toNum();
        n=MAX-MIN;
        printf("%04d - %04d = %04d
    ",MAX,MIN,n);
        if(n==0 || n==AIM)        //n是4个相同数字(包括0000的情况,不用额外考虑),或者剪到最后了
          break;
      }
      return 0;
    }
  • 相关阅读:
    HDU 4285
    Codeforces 242C
    Codeforces 811C
    Codeforces 883H
    Codeforces 371D
    Codeforces 926E
    牛客算法周周练17 解题报告
    牛客算法周周练17D
    牛客算法周周练17C
    牛客算法周周练17A
  • 原文地址:https://www.cnblogs.com/Evence/p/4316647.html
Copyright © 2011-2022 走看看