pat--1019 Digital Black hole __ Exercise

Source: Internet
Author: User
 given any number of 4-bit positive integers that are not exactly the same numbers, we get a new number if we first order 4 numbers in increments, then in non descending order, then subtract the 2nd number with the 1th digit.

To repeat this, we will soon stop at 6174, which is called the "digital Black Hole", which is also called the Kaprekar constant.

For example, starting with 6767, we will get 7766-6677 = 1089 9810-0189 = 9621 9621-1269 = 8352 8532-2358 = 6174 7641-1467 = 6174 ...

Now given any 4-bit positive integer, please write a program to demonstrate the process of reaching the black hole.

Input format: Enter a positive integer n within a (0, 10000) interval. Output format: If N's 4-bit numbers are all equal, output "n-n = 0000" In one line, otherwise each step of the calculation is output in one line until 6174 is shown as the difference, and the output format is shown in the sample.

Note that each digit is output in 4-digit format.
    Input Sample Example 1:6,767 Output Sample 1:7766-6677 = 1089 9810-0189 = 9621 9621-1269 = 8352 8532-2358 = 6174 Input Sample 2: 2222 Output Sample 2:2222-2222 = 0000 
#include <iostream> #include <algorithm> #include <string.h> #include <math.h> using namespace s

td
int sub;
Char a[4];
Char b[4];
    int main () {int m=0,n=0;
    cin>>a;
    int X=strlen (a);
        if (x<4) {for (int i=x;i<4;i++) {a[i]= ' 0 ';
    } int t=0;
        while (t!=6174) {sort (a,a+4);
        for (int i=0;i<4;i++) {b[i]=a[3-i];
            for (int i=0;i<4;i++) {m=m*10;
            n=n*10;
            m+= (a[i]-' 0 ');
        n+= (b[i]-' 0 ');
        } sub=n-m;
        T=sub;
        if (sub==0) {break;
        printf ("%04d-%04d =%04d\n", n,m,sub);
        m=0;n=0;
            for (int i=3;i>=0;i--) {a[i]=sub%10+ ' 0 ';
        sub/=10;
        } if (t==0) {printf ("%04d-%04d = 0000", n,m);
  cout<<n<< "-" <<m<< "= 0000";  return 0; }

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.