This is one of the less-complicated events, both to explain and to perform. In this event we’ll be working with a standard deck of playing cards. A standard deck consists of four suits: Hearts, Spades, Clubs, and Diamonds. Within each suit are the numbers two through ten, plus a Jack, a Queen, a King, and an Ace.
Given a random set of five cards, your task is to find out how many pairs are in that set. In other words, if your five cards are the 2 of hearts, the 4 of spades, the 4 of clubs, the queen of diamonds and the queen of spades, you have 2 pairs: 2 fours and 2 queens. As another example, you might have a 3 of clubs, a 3 of diamonds, a 3 of hearts, a 10 of spades and an ace of hearts. In that case you have 3 pairs: 3 of clubs and 3 of diamonds; 3 of diamonds and 3 of hearts; and 3 of clubs and 3 of hearts.
For this event you should assume you’ve been dealt the following five cards:
• | Seven of spades |
• | Five of hearts |
• | Seven of diamonds |
• | Seven of clubs |
• | King of clubs |
Using this set of cards, your script should display the number of pairs. Keep in mind that we will look at the scripts as we test them. A script that simply displays the number 3 will receive a score of 0; you actually have to do the calculations based on these cards. Not only that, but it shouldn’t matter what the cards are: if we substitute any other set of five cards your script should still return the correct number of pairs.
use strict;
use warnings;
my @card=('Seven of spades','Five of hearts','Seven of diamonds','Seven of clubs','King of clubs');
my @compare;
my $count;
foreach (@card) {
my @num=split/ /,$_;
push @compare,$num[0];
}
foreach (0..$#compare-1) {
my $i=$_;
foreach ($i+1..$#compare) {
if ($compare[$i] eq $compare[$_]) {
print "$card[$i] and $card[$_] \n";
$count++;
}
}
}
print "$count pair(s)";
'Programming > Perl' 카테고리의 다른 글
[Perl] SQL 스크립트에서 대상 정보 추출 (0) | 2014.12.07 |
---|---|
pl- 시저의 암호 (0) | 2009.05.16 |
Perl Underground 2 (0) | 2009.05.02 |
perl underground 1-3 (0) | 2009.05.02 |
perl underground 1-2 (0) | 2009.05.01 |
댓글