本帖最后由 523066680 于 2019-1-23 16:33 编辑
回复 22# search_Sudoku
按这个方式实现了(二分搜索没做进去),很利落- =info
- Ref:
- http://www.bathome.net/redirect.php?goto=findpost&ptid=51910&pid=217048
- =cut
-
- my @arr = qw/25 23 4 9 8 2 1 12 15/;
- my $offset;
- my $first;
- printf "%s <- Original\n", join(",", @arr);
-
- # 查找末尾有序数组的起点
- $offset = $#arr+1 - seqsizes( \@arr );
-
- while ( $offset > 0 || $arr[0] > $arr[1] )
- {
- $first = shift @arr;
- insert( \@arr, $offset, $first );
- $offset--;
- printf "%s\n", join(",", @arr);
- }
-
- sub insert
- {
- my ($arr, $begin, $ele) = @_;
- for my $i ( $begin .. $#$arr ) {
- if ($arr->[$i] > $ele ) { splice( @$arr, $i, 0, $ele ); return; }
- }
- splice( @$arr, $#$arr+1, 0, $ele );
- }
-
- sub seqsizes
- {
- my ($r, $size) = (shift, 1);
- for my $i (1..$#$r) {
- $r->[-$i] > $r->[-$i-1] ? $size++ : last;
- }
- return $size;
- }
复制代码
- 3,4,7,1,10,9,8,11,24 <- Original
- 4,7,1,10,9,3,8,11,24
- 7,1,10,9,3,4,8,11,24
- 1,10,9,3,4,7,8,11,24
- 10,9,1,3,4,7,8,11,24
- 9,1,3,4,7,8,10,11,24
- 1,3,4,7,8,9,10,11,24
复制代码
- 25,23,4,9,8,2,1,12,15 <- Original
- 23,4,9,8,2,1,12,15,25
- 4,9,8,2,1,12,15,23,25
- 9,8,2,1,4,12,15,23,25
- 8,2,1,4,9,12,15,23,25
- 2,1,4,8,9,12,15,23,25
- 1,2,4,8,9,12,15,23,25
复制代码
|