SRM257(2):BridgePts

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

    
Class: BridgePts
Method: pointValue
Parameters: int[]
Returns: int
Method signature: int pointValue(int[] hand)
(be sure your methodis public)
    
 
 

Constraints

- hand will contain exactly 13 elements, all distinct.
- Each element of hand will have a value between 1 and 52 inclusive.
 

Examples

0)  
    
{25,14,15,16,17,18,19,20,21,22,23,24,26}
Returns: 19
This hand contains all diamonds, so it has one Ace, one King, one Queeen, and one Jack, and it contains no cards in three suits. So its point value is 4 + 3 + 2 + 1 + 3 + 3 + 3 = 19.
1)  
    
{2,3,4,15,18,28,29,30,41,42,43,16,17}
Returns: 0
This hand contains only 2's, 3's, 4's and one 5. It has 3 or 4 cards in each suit.

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;
}

 

你可能感兴趣的:(Class,Parameters,constraints,each,iostream)