- 论坛徽章:
- 26
|
符合了 
2 2
1 1 2
1 1 1 1
2 2
2 2
1- #!/usr/bin/perl -w
- use 5.010;
- my @A = ( 2, 2, 1, 1, 2, 1, 1, 1, 2, 2, 1, 1, 2, 2 );
- my @B = GREP( 4, @A );
- say "@$_" for @B;
- sub GREP {
- my ( $TOTAL, @A ) = @_;
- my @RETURN = ();
- TRY:
- my @TMP = ();
- my $SUM = 0;
- my $TRY = 0;
- for (@A) {
- if ( $SUM + $_ > $TOTAL ) { next }
- push @TMP, $_;
- $SUM += $_;
- $_ = 0;
- if ( $SUM == $TOTAL ) {
- push @RETURN, [@TMP];
- $TRY = 1;
- $SUM = 0;
- @TMP = ();
- }
- }
- @A = grep { $_ } @A;
- if ($TRY) {
- # TOTAL = 5, A = [2, 1, 1, 2]
- # ERROR: push @A, @TMP
- @A = sort { $b <=> $a } @A, @TMP;
- goto TRY;
- }
- push @RETURN, [ @A, @TMP ];
- return @RETURN;
- }
复制代码 |
|