# 6174 of the problem __ Algorithm competition introductory Classic

Source: Internet
Author: User
In 1955, Capueca (D.r.kaprekar) studied a transformation of four-digit numbers: give a four digit k0, with its four digits from large to small rearrange into a four-digit m, subtract its inverse ordinal rev (m), draw several K1=m-rev (m), then, Continue to repeat the above transformation to K1, Count K2. So go on, Capueca found that no matter how big the k0 is four digits,

As long as the four numbers are not the same, up to 7 times above the transformation, there will be four digits 6174. For example:

k0=5298,k1=9852-2589=7263,k2=7632-2367=5265,k3=6552-2556=3996,k4=9963-3699=6264,k5=6642-2466=4176,k6=7641-1467 =6174.

Later, this issue was handed down, people call this problem "6174 problem", the transformation referred to above is called Capueca transformation, referred to as K transformation.

In general, as long as in the 0,1,2,..., 9 to take four of the number of unequal numbers to form an integer k0 (not necessarily four digits), and then from the K0 start to do the K transformation, the number of K1,K2,K3,..., will have a certain m (m=<7), so that km=6174.

More generally, from the 0,1,2,..., 9 to take n not all the same number to form a decimal number k0 (not necessarily n digits), and then, from K0 began to do K transformation, to get K1,K2,..., then what will the result be? It is now known that:

n=2, can only form a cycle: (27,45,09,81,63). For example, take two digits 7 and 3, and do a continuous k transformation, and conclude that: 36,27,45,09,81,27,... A loop occurs.

N=3, can only form a cycle: (495).

N=4, can only form a cycle: (6174).

N=5, three cycles have been found: (53855,59994), (62964,71973,83952,74943), (63954,61974,82962,75933).

N=6, three cycles have been found: (642654,...), (631764,...), (549945,...).

N=7, a loop has been found: (8719722,...).

N=8, four cycles have been found: (63317664), (97508421), (83208762,...), (86308632,...)

N=9, three cycles have been found: (864197532), (975296421,...), (965296431,...)

It is easy to prove that for any natural number n>=2, continuous k transformation must form a loop. This is because the number of n digits consists of only finite ones. But for n>=5, the number of loops and the length of the loop (the number of numbers in each loop) is unclear, This is also some of the domestic math enthusiasts are keen to study a topic.

Enter an n-digit number, sort all numbers from big to small to get a, get B from small to large, and replace the original number with A-b
and continues to operate. 1234, 4321-1234=3087, 8730-378=8352, 8532-2358=6174
Sample input: 1234
Sample output: 1234->3087->8352->6074->6174

```#include <stdio.h>
#include <stdlib.h>
#include <string>

//object function
int cmp (const void *_a, const void *_b)
{
char *a = (char *) _a;
Char *b = (char *) _b;

return *a-*b;
}
int getNext (int x)
{
int A;
int b;
int n;
Char s[100] = {0};
Digital Turn character
sprintf (S, "%d", x);
Character arrays are arranged
qsort (S,strlen (s), sizeof (S[0)), CMP);
The character array turns digital
sscanf (S, "%d", &a);
Flip character, Strrev is not an ANSI function (Standard C library function)
Strrev (s);
SSCANF (S, "%d", &b);

return b-a;
}

int num[2000];
int count;

int main (void)
{
scanf ("%d", &num[0]);
printf ("%d", num[0]);

Count = 1;
while (1)
{
Num[count] = GetNext (num[count-1]);
printf ("->%d", Num[count]);

int found = 0;
for (int i = 0; i < count; ++i)
{
if (num[i] = = Num[count])
{
found = 1;
break;
}
}
if (found) break
;
count++;
}
printf ("\ n");
return 0;
}```

Related Keywords:

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.