- 论坛徽章:
- 145
|
回复 1# liujie126
The other way to assign average number by call
use strict;
use warnings;
sub avg{
my($sAvg, @aData) = @_;
my @aRet;
my $sTotal = 0;
return if(@aData < $sAvg); # check it
$sTotal += $aData[$_] foreach(0 .. $sAvg-2); # precalculate the total without last one [0,1,2]
foreach(0 .. @aData-$sAvg){
$sTotal += $aData[$_ + $sAvg-1]; # summary with last one [0,1,2,3]
push @aRet, $sTotal/$sAvg; # to get average
$sTotal -= $aData[$_]; # remove the first one [1,2,3]
}
return @aRet;
}
while(<DATA>){
chomp;
my @aData = split;
my @aAvg = avg(4,@aData);
print "@aAvg\n";
}
__DATA__
1 1 1 -1 1 1 1 1 -1 1 1 -1 -1 1 -1 1 1 -1 -1 1 1 1 1 1 1 -1 1 1 1 1 1 -1 -1 1 1 -1 1 1 1 -1 1 1 1 1 -1 -1 1 -1 -1 -1 1 -1 1 -1 -1 1 1 1 -1 -1 1 1 1 1 1 1 1 -1 1 -1 1 -1 1 1 1 1 1 1 -1 -1 1 1 1 1 -1 1 -1 -1 -1 1 1 1 1 1 1 -1 1 1 1 1 1 -1 1 1 1 1 1 1 1 -1 -1 1 1 1 -1 -1 1 1 -1 -1 -1 1 1 -1 1 1 1 1 -1 1 -1 1 -1 1 1 -1 -1 1 1 1 1 1 1 1
1 1 1 1 -1 -1 -1 -1
|
|