C language · Digital Black Hole

Source: Internet
Author: User

Algorithm to improve the time limit of digital black hole: 1.0s memory limit: 256.0MBThe problem describes any four-digit number, as long as the numbers on each bit are not identical, there is this rule:
1) the four digits comprising the four digits are arranged from large to small, forming the largest four digits consisting of these four numbers;
2) the four digits comprising the four digits are arranged from small to large, forming the smallest four digits consisting of the four digits (if four are included, the resulting number is less than 0 digits);
3) ask for two number difference, get a new four digit number (high 0 reserved).
Repeat the above process and the result will be 6174.
For example: 4312 3087 8352 6174, after three transformations, get 6174 input format a four-bit integer, enter a guaranteed four-bit number is not the same output format an integer, indicating how many times the number of transformations can get 6174 sample input 4312 sample Output 3 purpose clear, test instructions very clear.
1#include <stdio.h>2#include <string.h>3#include <stdlib.h>4 intans=0;5 voidShengintA[]) {6      for(intI=0;i<4-1; i++){7          for(intj=0;j<4-i-1; j + +){8             if(a[j]>a[j+1]){9                 intt =a[j];Tena[j] = a[j+1]; onea[j+1] =t; a             } -         } -     } the } - voidJiangintA[]) { -      for(intI=0;i<4-1; i++){ -          for(intj=0;j<4-i-1; j + +){ +             if(a[j]<a[j+1]){ -                 intt =a[j]; +a[j] = a[j+1]; aa[j+1] =t; at             } -         } -     } - } - intTomax (intNum) { -     inta[4]={0}; ina[0] = num/ +%Ten;//thousand -a[1] = num/ -%Ten;//Hundred toa[2] = num/Ten%Ten;//10 Guests +a[3] = num%Ten;//Digit - Jiang (a); the     returna[0]* ++a[1]* -+a[2]*Ten+a[3]; * } $ intTomin (intNum) {Panax Notoginseng     inta[4]={0}; -a[0] = num/ +%Ten;//thousand thea[1] = num/ -%Ten;//Hundred +a[2] = num/Ten%Ten;//10 Guests aa[3] = num%Ten;//Digit the Sheng (a); +     returna[0]* ++a[1]* -+a[2]*Ten+a[3]; - } $ voidDfsintNum) { $     if(num==6174){ -         return; -     } theNum=tomax (num)-tomin (num); -ans++;Wuyi DFS (num); the } - intmain () { wu     intnum; -scanf"%d",&num); about DFS (num); $printf"%d", ans); -     return 0; -}

C language · Digital Black Hole

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.