Thread Project Euler
(56 answers)
Opened by Ronnie at 2007-12-15 12:19
So , hab das nochmal getestet und die while-Schleife (hoffentlich richtig) eingebaut, laut den Testausgaben laeuft das Script richtig.
Code (perl): (dl
)
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 #!/usr/bin/perl -w use strict; my @table; for (<DATA>) { last if ($_ =~ m/^###/); chomp; push @table, [split / /]; } my %highest = (a => [0,0], b => [0,0], c => [0,0], d => [0,0], sum => 0); for (my $row=0; $row < $#table; $row++) { for (my $col=0; $col <= $#{$table[0]}; $col++ ) { get_highest($row, $col); } } print "The four coherent numbers, which have the highest product:\n"; for my $key (sort keys %highest) { next if ($key eq 'sum'); print "$key => [$highest{$key}->[0],$highest{$key}->[1]] = $table[$highest{$key}->[0]]->[$highest{$key}->[1]]\n"; } sub get_highest { my ($row, $col) = @_; my @steps = qw/c- c-r- r- c+r+ c+ c+r- r+ c-r+/; while (my $step = shift @steps) { my ($tmp_row, $tmp_col) = ($row, $col); my $sum = 0; my @cur_coords; print "Check [$row, $col]\n"; for (0..3) { last unless $table[$tmp_row]->[$tmp_col]; print "S $step\t| R $tmp_row | C $tmp_col\n"; $sum += $table[$tmp_row]->[$tmp_col]; push @cur_coords, [$tmp_row, $tmp_col]; $tmp_row-- if($step =~ m/r\-/); $tmp_col-- if($step =~ m/c\-/); $tmp_row++ if($step =~ m/r\+/); $tmp_col++ if($step =~ m/c\+/); } if ( ($sum > $highest{'sum'}) && (scalar @cur_coords > 3) ) { $highest{'sum'} = $sum; my $i=0; for my $key (sort keys %highest) { next if ($key eq 'sum'); $highest{$key} = [$cur_coords[$i]->[0], $cur_coords[$i]->[1]]; $i++; } } } } #data siehe oben __DATA__ Ausgabe: Code: (dl
)
1 Check [19, 18] Hab ich irgendwo n Denkfehler? Pörl.
|