-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp100.c
55 lines (41 loc) · 833 Bytes
/
p100.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
* https://www.aceptaelreto.com/problem/statement.php?id=100
*/
#include <stdio.h>
#include <stdlib.h>
int order(const void* a, const void* b);
int kaprekar_constant(int val);
int
main(int argc, char**argv) {
int it, r_it, val;
scanf("%d", &r_it);
for (it=0; it<r_it; ++it) {
scanf("%d", &val);
printf("%d\n", kaprekar_constant(val));
}
return 0;
}
int
kaprekar_constant(int val) {
char max_val[5], min_val[5];
int i, num = 0;
min_val[4] = '\0';
while (val != 6174) {
sprintf(max_val, "%04d", val);
qsort(max_val, 4, sizeof(char), order);
for (i=0; i<4; ++i)
min_val[i] = max_val[3-i];
val = atoi(max_val) - atoi(min_val);
if (val == 0)
return 8;
++num;
}
return num;
}
int
order(const void* a, const void* b) {
if (*((char*)a) > *((char*)b))
return -1;
else
return 1;
}