脚本编写比赛 (1 扑克牌配对)
时间:2008-03-03 来源:starB6
#!/usr/bin/perl -w
use strict;
# spades7 hearts5 diamonds5 clubs7 clubsK
#参赛人员将得到一系列五张一组的扑克牌,要求确定配对数。
my @cards=qw/spades7 hearts7 diamonds5 clubs7 clubsK/ ;
#定义一个hash
my $key;
my $value;
my $number=0;
my %hashcard = ("1",0,"2",0,"3",0,"4",0,"5",0,"6",0,"7",0,"8",0,"9",0,"10",0,"J",0,"Q",0,"K",0,"King",0);
while(( $key,$value)=each(%hashcard))
{
# print "$key $value\n";
foreach(@cards)
{
my $num=substr $_,-1 ,1;
if ($num eq $key)
{
$hashcard{$key}+=1;
print "$key ,$hashcard{$key}\n";
}
}
}
foreach my $key(keys(%hashcard))
{
print "$key = $hashcard{$key}\n";
if ( $hashcard{$key} >= 2 )
{
$number+=1;
print "number:$number\n";
}
}
print " the cards can make pairs in them is :\n ";
print "配对的数目为:$number\n";
use strict;
# spades7 hearts5 diamonds5 clubs7 clubsK
#参赛人员将得到一系列五张一组的扑克牌,要求确定配对数。
my @cards=qw/spades7 hearts7 diamonds5 clubs7 clubsK/ ;
#定义一个hash
my $key;
my $value;
my $number=0;
my %hashcard = ("1",0,"2",0,"3",0,"4",0,"5",0,"6",0,"7",0,"8",0,"9",0,"10",0,"J",0,"Q",0,"K",0,"King",0);
while(( $key,$value)=each(%hashcard))
{
# print "$key $value\n";
foreach(@cards)
{
my $num=substr $_,-1 ,1;
if ($num eq $key)
{
$hashcard{$key}+=1;
print "$key ,$hashcard{$key}\n";
}
}
}
foreach my $key(keys(%hashcard))
{
print "$key = $hashcard{$key}\n";
if ( $hashcard{$key} >= 2 )
{
$number+=1;
print "number:$number\n";
}
}
print " the cards can make pairs in them is :\n ";
print "配对的数目为:$number\n";
相关阅读 更多 +