blob: 8d92f1cd07713f3fe38f576eb93577a313232921 (
plain)
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
|
import std.stdio:writeln,write;
import std.algorithm:sort;
int product(int n)
{
int prod = 1;
while(n)
{
prod *= n % 10;
n /= 10;
}
return prod;
}
int helper(int n)
{
int sum = 0;
while(n >= 10)
{
sum++;
n = product(n);
}
return sum;
}
bool compare(int a, int b)
{
int ha = helper(a);
int hb = helper(b);
return ha == hb ? a < b : ha < hb;
}
void persistence_sort(ref int[] arr)
{
arr.sort!(compare);
}
void main()
{
int[] arr1 = [15,99,1,34];
int[] arr2 = [50,25,33,22];
persistence_sort(arr1);
persistence_sort(arr2);
writeln(arr1);
writeln(arr2);
}
|