sub sum{
$total = 0;
@arr = @_;
for $item(@arr){
$total +=$item;
}
return $total;
}
#contiNum(number)
sub contiNum
{
$num = scalar($_[0]);
for $start(1 .. $num)
{
for $end($start.. ($num-1))
{
@range=($start .. $end);
if (sum(@range)==$num){
print "$start+ .. +$end = $_[0]\n";
#可改用下列註解的程式碼得到更好的輸出表示
#@solutionConti = ($start .. $end);
#$addAll = join("+",@solutionConti);
#print "$addAll = $_[0]\n";
}
}
}
#$start from 1 to $num
#$end from 1 to $num-1
}
contiNum(18);
沒有留言:
張貼留言