0

私のコードは最初の数回の反復では機能しますが、while ループを数回繰り返した後、配列要素が削除されているように見えます。

入力パラメーターから構築された配列から数値を取得していますが、2 回渡された数値に到達するとエラーが発生するということしかわかりません。

私はこのようにスクリプトを呼び出しています

./branchandboundNoComments.pl 1 2 3 4 5 5 7 7 9 9 10 10 12 14 19

これを出力として取得する必要があります

0, 7, 9, 10, 14, 19

これは私のスクリプトです

#!/usr/bin/perl -w 

use strict;

my @input  = @ARGV;
my $maxAll = $input[-1];
$#input = $#input - 1;
my @multiset = ( 0, $maxAll );
my @stack;

my $rotation = 0;    # this is 0,1, or 2.

while ( @input != 0 ) {

    my $max = $input[-1];

    my @deltamultiset;
    for ( my $i = 1; $i <= $#multiset; $i++ ) {
        push @deltamultiset, $multiset[$i] - $max;
    }
    push @deltamultiset, $max;

    my @deltamultiset2;
    for ( my $i = 1; $i <= $#multiset; $i++ ) {
        push @deltamultiset2, $multiset[$i] - ( $maxAll - $max );
    }
    push @deltamultiset2, $max;

    if ( subset( \@deltamultiset, \@input ) and $rotation == 0 ) {

        for ( my $i = 0; $i < $#deltamultiset; $i++ ) {
            pop @input;
        }

        push @multiset, $max;
        push @stack,    $max;
        push @stack,    0;
    }
    elsif ( subset( \@deltamultiset2, \@input ) and $rotation <= 1 ) {

        for ( my $j = 0; $j < $#deltamultiset; $j++ ) {
            pop @input;
        }

        push @multiset, ( $maxAll - $max );
        push @stack,    ( $maxAll - $max );
        push @stack, 1;
        $rotation = 0;
    }
    elsif ( @stack != 0 ) {

        $rotation = $stack[-1];
        $#stack--;
        $max = $stack[-1];
        $#stack--;
        $rotation++;

        for ( my $i = 0; $i < $#multiset; $i++ ) {
            if ( $multiset[$i] == $max ) {
                delete $multiset[$i];
                last;
            }
        }

        for ( my $i = 0; $i < $#deltamultiset; $i++ ) {
            push @input, $deltamultiset[$i];
        }
    }
    else {
        print "no solutions \n";
        exit;
    }
}

print "@multiset is a solution \n";

sub subset {
    my ( $deltamultisetSubref, $multisetSubref ) = @_;
    my @deltamultisetSub = @{$deltamultisetSubref};
    my @multisetSub      = @{$multisetSubref};

    while ( @deltamultisetSub != 0 ) {

        for ( my $i = $#multisetSub; $i >= -1; $i-- ) {

            if ( $multisetSub[$i] == $deltamultisetSub[-1] ) {
                pop @deltamultisetSub;
                $#multisetSub--;
                last;
            }

            if ( $i == -1 ) {
                return 0;
            }
        }
    }

    return 1;
}

これが出力されるものです

Use of uninitialized value in subtraction (-) at ./branchandboundNoComments.pl line 20.
Use of uninitialized value in subtraction (-) at ./branchandboundNoComments.pl line 26.
no solutions 
4

1 に答える 1

1

あなたが実装しようとしているアルゴリズムを理解できないので、おそらくもっと多くのエラーがありますが、差し迫った問題は、ステートメントが

delete $multiset[$i]

最後の要素でない限り、配列からその要素を削除しません。それ以外の場合、配列は同じ長さのままで、その要素ではfalseexistsが返され、次のように評価されます。undef

最も可能性が高いと思われる要素を削除したい場合は、

splice @multiset, $i, 1;

しかし、その修正を行ってコードをテストしましたが、減算エラー で初期化されていない値の使用が生成されなくなりましたが、結果はまだ解決策ではありません

残念ながら、あなたが何を実装しようとしているのか理解できず、基礎となるアルゴリズムの説明を私に提供できない限り、何が間違っているのかについて有益な推測をすることはできません.

于 2016-12-13T13:32:54.480 に答える