Problem Statement |
|||||||||||||
A deck of cards contains 52 cards. Each card has a suit (Clubs,Diamonds,Hearts,Spades) and a value (Ace,2,3,...,9,10,Jack,Queen,King). In the game of bridge a hand consists of 13 cards from the deck. A player needs to evaluate his hand, giving it a point value. The standard method is as follows: count 4 points for each Ace, 3 points for each King, 2 points for each Queen, and 1 point for each Jack. For each suit, count 1 point if the hand contains exactly two cards of that suit, 2 points if exactly one card, and 3 points if the hand contains no cards of that suit. The point value of the hand is the sum of all these points. Create a class BridgePts that contains a method pointValue that is given a int[] hand and that returns the point value of the hand. Each element of hand indicates a card. The clubs are numbered 1 to 13, the diamonds are 14 to 26, the hearts are numbered 27 to 39, and the spades are numbered 40 to 52. Within each suit, the cards are numbered in the order Ace, 2, 3, ..., 9, 10, Jack, Queen, King. So, for example, the King of Hearts is numbered 39 and the Ace of Spades is numbered 40. |
|||||||||||||
Definition |
|||||||||||||
|
|||||||||||||
Constraints |
|||||||||||||
- | hand will contain exactly 13 elements, all distinct. | ||||||||||||
- | Each element of hand will have a value between 1 and 52 inclusive. | ||||||||||||
Examples |
|||||||||||||
0) | |||||||||||||
|
|||||||||||||
1) | |||||||||||||
|
My Solution:
(ccnupq 2006-04-19 http://blog.csdn.net/ccnupq/)
#include <iostream>
#include<vector>
using namespace std;
class BridgePts
{
public:
int pointValue(vector<int> hand);
};
int BridgePts::pointValue(vector<int> hand)
{
int i;
int point=0;
vector<int> temp(4,0);
for(i=0;i<=12;i++)
{
if(hand[i]>=1&&hand[i]<=13)
temp[0]++;//club
else if(hand[i]>=14&&hand[i]<=26)
temp[1]++;//diamond
else if(hand[i]>=27&&hand[i]<=39)
temp[2]++;//heart
else
temp[3]++;//spades
switch(hand[i]%13)
{
case 1 :point+=4;break;
case 11:point+=1;break;
case 12:point+=2;break;
case 0 :point+=3;break;
}
}
for(i=0;i<=3;i++)
{
switch(temp[i])
{
case 0:point+=3;break;
case 1:point+=2;break;
case 2:point+=1;break;
}
}
return point;
}
int main()
{
BridgePts BridgePtsObj;
int num;
int a[]={25,14,15,16,17,18,19,20,21,22,23,24,26};
vector<int> hand(a,a+13);
num=BridgePtsObj.pointValue(hand);
cout<<num;
return 0;
}