From ab5ca0309aeb72e6373df1935ed46f4d77945ecd Mon Sep 17 00:00:00 2001 From: Fung Cheok Yin <61836418+E7-87-83@users.noreply.github.com> Date: Fri, 17 Apr 2020 21:15:28 +0800 Subject: Add files via upload --- ch-1.pl | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 ch-1.pl diff --git a/ch-1.pl b/ch-1.pl new file mode 100644 index 0000000000..f33a6aff99 --- /dev/null +++ b/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/bin/perl +use strict; + +# Usage: $ ch-1.pl (number of terms) (diff-k) (LIST of N sorted integers, from small to large) +# ch-1.pl 3 1 1 2 3 + +my $N = shift @ARGV; +my $DIFFK = shift @ARGV; + +die "not enough terms or too many terms" if $N != $#ARGV+1; + +my @A = @ARGV; + +my @B; + +for (1..$N-1) { + push @B, $A[$_] - $A[$_-1]; +} + +for (0..$N-2) { + my $diff = 0; + my $pointer = $_+1; + while ($pointer < $N and $diff <= $DIFFK) { + $diff += $B[$pointer-1]; + print $pointer, " ", $_, "\n" if $diff==$DIFFK; + $pointer++; + } +} -- cgit