Chinaunix首页 | 论坛 | 博客
  • 博客访问: 89104
  • 博文数量: 99
  • 博客积分: 55
  • 博客等级: 民兵
  • 技术积分: 510
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-20 21:29
文章分类

全部博文(99)

文章存档

2013年(5)

2012年(94)

我的朋友

分类:

2012-06-09 13:02:25


use strict;
print "Please input a ROMA number:\n";

while(<STDIN>)
{
    chomp;
    my $roma=$_;
    if($roma=~ m/^[IVXLCDM]+$/i)
    {
        my %hash=("I"=>0,"V"=>1,"X"=>2,"L"=>3,"C"=>4,"D"=>5,"M"=>6);
        my @value=(1,5,10,50,100,500,1000);
        $roma=uc($roma);
        my @roma=split '',$roma;
        my ($result,$i,$flag);

        $result=$value[$hash{$roma[0]}] if($i==$#roma);


        while($i<$#roma)

        {
            if($hash{$roma[$i]}<$hash{$roma[$i+1]})
            {
                my $id;
                if($hash{$roma[$i]}%2 ==0)
                {
                    $id=3;
                }else
                {
                    $id=2;
                }

                if(($hash{$roma[$i+1]}-$hash{$roma[$i]})<$id)
                {
                    $result+=$value[$hash{$roma[$i+1]}]-$value[$hash{$roma[$i]}];
                    $i+=2;

                }else
                {
                    print "Invalid number, wrong oder or grammer.\n";
                    last;
                }
            }else
            {
                $result+=$value[$hash{$roma[$i]}];

                if($roma[$i+1] eq $roma[$i])
                {
                    $flag++;
                }else
                {
                    $flag=0;
                }

                if($flag>2)
                {
                    print "Invalid number, wrong grammer.\n";
                    last;
                }
                $i++;

            }

        }

        if($i==$#roma)

        {
            $result += $value[$hash{$roma[-1]}];    
            $i++;
        }

        if(($i==$#roma+1)||($#roma == 0))

        {
            print "$roma = $result\n";
        }

    }else
    {
        print "Please input the valid Roma number\n";
    }
}


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