可以处理12的倍数,写的很乱,到了720就变得很慢了:- #!/usr/bin/perl
- use strict;
- use warnings;
- print "请输入12的倍数:";
- chomp (my $digit=<STDIN>);
-
-
- use List::Util qw(sum);
- my $i=1;
- my $half_digit=$digit/2;
- my $half_sum=sum(1..$digit)/2;
- my $half_1=$half_sum+1;
- while ($i <= $half_1) {
- my $result=sum($i++..$half_digit++);
- print join " ",$i-1..$half_digit-1 if $half_sum==$result;
-
- }
- print "\n\n\n";
-
- $i=$digit/2+2;
- $half_digit=$digit/2;
- while ($i<=$digit) {
- my $result=sum(1..$digit)-sum($i-$half_digit..$i-1);
- my @num=($i..$digit);
- print join " ",@num,1..$half_digit-@num if $half_sum==$result;
- $i++;
-
- }
复制代码
|