程序员的算法趣题Perl版(三)

第七题

将日期写成YYYYMMDD的形式,然后转换成二进制,并逆序排列,然后再转换成十进制,如果与之前的日期相等则正确。查找1970-01-01到2020-07-24之间正确的日期。


#! perl

#20171108

use Date::Parse;
use strict;

my $startTime = "1 Jan 1970";
my $endTime = "24 Jul 2020";

my $startStamp = str2time($startTime);
my $endStamp = str2time($endTime);

for(my $i = $startStamp; $i <= $endStamp; $i = &nextDay($i)) {

  &isRight($i);
}

sub nextDay {

    my $date = @_[0];
    my $dateNext = $date + 86400;
    return $dateNext;
}

sub isRight {

    my $date = @_[0];

    my($sec,$min,$hour,$day,$mon,$year) = localtime($date);   
    $mon++;$year += 1900;  
    my $date_now = sprintf("%04d%02d%02d",$year,$mon,$day);

    my $dateBin = sprintf("%b", $date_now);

    my $dateBinRev = reverse $dateBin;

    my $dateDnow = oct('0b' . $dateBinRev);

    if($date_now eq $dateDnow) {

        print $date_now, "\n";
    } 

    return 0; 
}

第八题

机器人可以前后左右移动,每次移动一步,不可以重复到达同一点。比如连续移动3次,则一共有36中路径。
求:连续移动12次,有多少种路径。

#!perl

#20171113

use strict;

# The 4 directions
my %direction = ('up' => '(0,1)',
                'down' => '(0,-1)',
                'left' => '(-1,0)',
                'right' => '(1,0)');

# This method is used to get the total steps of something like a robot has moved, 
# the robot can only move forward, backward, left and right with one step a time. 
# And one place to go only once. 
sub forward {

    my $p = @_[0];

    my @path = @$p;

    my $step = 0;
    my $length = @path;

    # The robot can only move 12 steps;
    if($length == 13) {

        return 1;
    }

    if(@path) {

        # direction loop
        foreach(values %direction) {

            my $temp = $_;

            # forward 
            $temp = &move(@path[-1], $_);

            # check the target place whether the robot has been to it
            if(! grep {$_ eq $temp} @path) {# if not, move

                # record the place
                push @path,$temp;

                # enter the iteration process
                $step += &forward(\@path);

                # pop the lastest place when the robot has explored one path
                pop @path;
            }

        }

        return $step;
    } 
}

my @path = qw/(0,0)/;
my $pp = \@path;

my $result = &forward($pp);
print "result $result \n";

# // this method is used to change the location
sub move {

    my $src = @_[0];
    my $tar = @_[1];

    my @srcArr = split /,/, $src;

    my $src_x = substr $srcArr[0], 1;
    my $src_y = substr $srcArr[1], 0;

    my @tarArr = split /,/, $tar;
    my $tar_x = substr $tarArr[0], 1;
    my $tar_y = substr $tarArr[1], 0;

    my $res_x = $src_x + $tar_x;
    my $res_y = $src_y + $tar_y;

    my $res = "(" . $res_x . "," . $res_y . ")";

    $res;
}

这道题用到了深度遍历算法,该算法的意思就是:一条道走到黑,撞墙之后回到上一个十字路口,换个方向继续走,一直到走完所有的路或者达到停止条件。

ps: 英文注释是为了提高自己的英文写作和阅读水平,以后还要多多练习

第九题

没明白题目…两组人数均等是什么意思?男女相等么?男生20人,女生10人,怎么分也不会均等啊。可能是我理解有问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值