本帖最后由 523066680 于 2015-6-4 20:36 编辑
- use utf8;
- binmode(STDOUT, ":encoding(gbk)");
-
- my @cup;
- my @element;
-
- our %h=(
- "father" => { "sex"=>"male", "age"=>100 },
- "mother" => { "sex"=>"female", "age"=>90 },
- "son" => { "sex"=>"male", "age"=>20 },
- "daughter" => { "sex"=>"female", "age"=>20 },
- );
-
- @element = keys %h;
-
- &arrange(\@cup, \@element);
-
- sub arrange {
- my ($a, $b) = (shift, shift);
- my (@ar, @br);
-
- if (scalar(@{$a}) == 4) {
- &check(@{$a});
- }
- for my $i (0..$#{$b}) {
- @ar=(@{$a}, $b->[$i]);
- @br=@{$b}[0..$i-1, $i+1..$#{$b}];
- &arrange(\@ar, \@br);
- }
- }
-
- sub check {
- our %h;
- my ($killer, $victim, $helper, $witness) = @_;
- return 0 if ( $h{$helper}{sex} eq $h{$witness}{sex} ); # 1 帮凶和目击者性别不同
- return 0 if ( $h{father}{sex} eq $h{$witness}{sex} ); # 2 最年长者和目击者性别不同
- return 0 if ( $h{$helper}{age} < $h{$victim}{age} ); # 4 帮凶年纪 大于 被害者年纪
-
- # 3 最年轻者和被害者性别不同
- # 6 最年轻的不是凶手
-
- if ( $h{$victim}{"sex"} eq "female" ) { # 若被害者是女性
- return 0 if ( $killer eq "son" ); #则最年轻的是男性(儿子)
- $h{son}{age} = 10; #假设年龄
- $h{daughter}{age} = 20; #假设年龄
- } else { # 相反
- return 0 if ( $killer eq "daughter" );
- $h{son}{age} = 20;
- $h{daughter}{age} = 10;
- }
-
- return 0 if ( $h{$helper}{age} < $h{$victim}{age} );
-
- print "凶手:$killer 受害者:$victim, 帮凶:$helper, 目击者:$witness\n";
- return 1;
- }
复制代码 arrange 是递归排列
凶手:mother 受害者:son, 帮凶:father, 目击者:daughter
在某个群里一问,人家十来秒就把答案手算出来了 T_T 突然感觉自己好渣 |