about summary refs log tree commit diff
path: root/daily/287easy/kaprekar.c
blob: 0cf1bf60ad544a60382968079538aa8668cde941 (plain) (blame)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <stdio.h>

char a[4];

char largest_digit(short n) 
{
	char m = n / 1000;

	if (m < n % 1000 / 100)
		m = n % 1000 / 100;

	if (m < n % 100 / 10)
		m = n % 100 / 10;

	return (m < n % 10) ? n % 10 : m;
}

void sort_digits(short n)
{
	char i, j;

	a[0] = n / 1000;
	a[1] = n % 1000 / 100;
	a[2] = n % 100 / 10;
	a[3] = n % 10;

	for (i = 0; i < 3; i++)
		for (j = i + 1; j < 4; j++)
			if (a[i] < a[j]) {
				a[i] += a[j];
				a[j] = a[i] - a[j];
				a[i] -= a[j];
			}
}

short desc_digits(short n)
{
	sort_digits(n);

	return a[0] * 1000 + a[1] * 100 + a[2] * 10 + a[3];
}

unsigned long long kaprekar(short n)
{
	char i;

	for (i = 0; n ^ 6174; i++) {
		sort_digits(n);
		n = (a[0] - a[3]) * 999 + (a[1] - a[2]) * 90;
	}

	return i;
}

int main()
{
	printf("largest_digit(1234) -> %hhd\n", largest_digit(1234));
	printf("largest_digit(3253) -> %hhd\n", largest_digit(3253));
	printf("largest_digit(9800) -> %hhd\n", largest_digit(9800));
	printf("largest_digit(3333) -> %hhd\n", largest_digit(3333));
	printf("largest_digit(120) -> %hhd\n\n", largest_digit(120));

	printf("desc_digits(1234) -> %hd\n", desc_digits(1234));
	printf("desc_digits(3253) -> %hd\n", desc_digits(3253));
	printf("desc_digits(9800) -> %hd\n", desc_digits(9800));
	printf("desc_digits(3333) -> %hd\n", desc_digits(3333));
	printf("desc_digits(120) -> %hd\n\n", desc_digits(120));

	printf("kaprekar(6589) -> %lld\n", kaprekar(6589));
	printf("kaprekar(5455) -> %lld\n", kaprekar(5455));
	printf("kaprekar(6174) -> %lld\n", kaprekar(6174));

	return 0;
}