Thread Rätsel: mit Perl lösbar? (22 answers)
Opened by lichtkind at 2005-01-13 16:50

esskar
 2005-01-16 23:45
#19409 #19409
User since
2003-08-04
7321 Artikel
ModeratorIn

user image
mein code hat hinten und vorne nicht gefunzt;
hier mal ne bessere version
Code: (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
58
59
60
61
62
63
#!/usr/bin/perl

use strict;
use warnings;

my @Eq = (
" ABC + DDE = FCF",
" CB + BGE = BCA",
" AEE + CBG = DGAE",
" ABC + CB = AEE",
" DDE + BGE = CBG",
" FCF + BCA = DGAE",
);

&Sol(@Eq);

sub Sol
{
my (@e) = @_;

my %pairs = ();

for (@e)
{
s!=!==!;
$pairs{$_} = 0 foreach (m!(\w)!g);
}

my @vars = keys %pairs;
my $count = scalar @vars;

while(1)
{
my $true = 0;
my @out = ();
foreach (@e)
{
my $e = $_;
while(my($k, $v) = each %pairs)
{

$e =~ s!$k!$v!g;
}

$true = eval $e;
last unless $true;

push @out, $e;
}

print join("\n", @out,"\n") if $true;

my $i = 0;
while($i < $count)
{
last if ++$pairs{$vars[$i]} < 10;
$pairs{$vars[$i]} = 0;
$i++;
}

last if $i == $count;
}
}

View full thread Rätsel: mit Perl lösbar?