- 论坛徽章:
- 5
|
本帖最后由 pitonas 于 2014-08-21 12:14 编辑
因此,这是我的代码 ;- #!/usr/bin/perl
- while (<DATA>) {
- my ( $K, $N ) = split;
- my ( $K_sum, @mind );
- $K_sum += $_ for split //, $K;
- for my $i ( 1 .. $N ) {
- my $sum;
- $sum += $_ for split //, $i;
- push @{ $mind[$sum] }, $i;
- }
- my $index = 0;
- $index += @$_ for @mind[ 1 .. $K_sum - 1 ];
- for my $n ( sort { $a cmp $b } @{ $mind[$K_sum] } ) {
- last if $K == $n;
- $index++;
- }
- my ( $INDEX, $I );
- for my $i ( @mind[ 1 .. $#mind ] ) {
- last if $INDEX > $K;
- $I++;
- $INDEX += @$i;
- }
- $mind[$I] = [ sort { $a cmp $b } @{ $mind[$I] } ];
- my $number = $mind[$I][ $K - $INDEX ];
- print join( "\t", $K, $N, $index, $number ), "\n";
- }
- __DATA__
- 10000 100000
复制代码 change it for speed test
__DATA__
10000 10000000 |
|