Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1419661
  • 博文数量: 239
  • 博客积分: 5909
  • 博客等级: 大校
  • 技术积分: 2715
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-24 20:19
文章分类

全部博文(239)

文章存档

2014年(4)

2013年(22)

2012年(140)

2011年(14)

2010年(59)

我的朋友

分类:

2010-11-30 21:41:29

#use warnings;

use strict;
use Switch;

my @set=('M','D','C','L','X','V','I');

while(<STDIN>)
{
    chomp;
    my $number=$_;
    my $i=0;
    my @array;
    my $result;
    if($number=~ m/^[1-3]??(?(?<=^)[1-9]|\d){0,3}$/)
    {
        $number=sprintf "%4d",$number;
        my @num=split "",$number;
        foreach my $num (@num)
        {
            unless(($num eq ' ')||($num eq '0')){
                switch($i)
                {
                    case 0
                    {
                        @array=convert('','',$set[0]);
                    }
                    case 1
                    {
                        @array=convert($set[0],$set[1],$set[2]);
                    }
                    case 2
                    {
                        @array=convert($set[2],$set[3],$set[4]);
                    }
                    case 3
                    {
                        @array=convert($set[4],$set[5],$set[6]);
                    }
                }
                $result.=$array[$num-1];
            }
            $i++;
        }
        print "$result\n";
        
    }else
    {
        print "Please input a number between 1 and 3999\n";    
    }
}

sub convert
{
    my($first,$second,$third)=@_;
    my ($temp,@res);
    foreach(1..9)
    {
        if($_<4)
        {
            $temp="$third"x"$_";
        }
        elsif($_ == 4)
        {
            $temp=$third.$second;
        }
        elsif($_<9)
        {
            my $time=$_-5;
            $temp=$second."$third"x"$time";
        }else
        {
            $temp=$third.$first;
        }
        push @res,$temp;
    }
    return @res;
}


阅读(1534) | 评论(1) | 转发(1) |
给主人留下些什么吧!~~

chinaunix网友2010-12-01 15:08:27

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com