blob: 5c67abf85308d03ab02f74c81cfefa921b3055e6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
#!/usr/bin/env perl
use strict;
use warnings;
use Algorithm::Combinatorics qw(permutations subsets);
sub largest_of_three{
my $res = -1;
foreach my $i(0..@{$_[0]}){
foreach my $subset(subsets $_[0],$i){
foreach my $p(permutations $subset){
next unless @$p;
my $n = join '',@$p;
$res = $n if $n > $res && $n % 3 == 0;
}
}
}
$res
}
printf "%d\n",largest_of_three([8,1,9]);
printf "%d\n",largest_of_three([8,6,7,1,0]);
printf "%d\n",largest_of_three([1]);
|