blob: 2ab91e554a8686cb08ab0c598e67eddf0c4181a1 (
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
|
#!/usr/bin/perl
#
# Note: As $a and $b are special variables in Perl, I've used
# $x, $y and $z instead of $a, $b and $c.
use strict;
use warnings;
use feature 'say';
my $target = 0;
my @numbers = sort { $a <=> $b } grep { /^-?\d+$/ } @ARGV;
die "Usage: $0 [list of integers]\n" unless @numbers;
for my $i (0 .. $#numbers - 1) {
my $x = $numbers[$i];
my $start = $i + 1;
my $end = $#numbers;
while ($start < $end) {
my $y = $numbers[$start];
my $z = $numbers[$end];
if ($x + $y + $z == $target) {
say "($x, $y, $z)";
$start++;
$end--;
} else {
if ($x + $y + $z > $target) {
$end--;
} else {
$start++;
}
}
}
}
|