- 论坛徽章:
- 307
|
本帖最后由 sunzhiguolu 于 2016-09-11 22:00 编辑
- #!/usr/bin/perl
- use strict;
- use warnings;
- sub Sum{
- my $sum = 0;
- $sum += $_ for @{$_[0]};
- return $sum == 4 ? 0 : do {$sum > 4 ? 1 : -1};
- }
- my $aNums = [2, 1, 1, 1, 2, 1, 1, 2, 2, 2, 1, 1, 1];
- my @aData = [splice (@{$aNums}, 0, 2)];
- my $flag;
- while (@$aNums){
- last if (Sum ([@$aNums, @{$aData[-1]}]) == -1);
- if ($flag = Sum ($aData[-1])){
- $flag > 0 ? push (@$aNums, shift (@{$aData[-1]})) : push (@{$aData[-1]}, shift (@$aNums));
- }
- push (@aData, [splice (@{$aNums}, 0, 2)]) if (!Sum ($aData[-1]));
- }
- print "@$_\n" for @aData, $aNums;
复制代码
perl abc.pl
-----------------------------------
2 1 1
1 2 1
2 2
2 1 1
1 1
|
|