TopCoder算法竞赛题2:SRM 146 DIV 2, 250-point

Problem Statement

This task is about the scoring in the first phase of the die-game Yahtzee, where five dice are used. The score is determined by the values on the upward die faces after a roll. The player gets to choose a value, and all dice that show the chosen value are considered active. The score is simply the sum of values on active dice.

Say, for instance, that a player ends up with the die faces showing 2, 2, 3, 5 and 4. Choosing the value two makes the dice showing 2 active and yields a score of 2 + 2 = 4, while choosing 5 makes the one die showing 5 active, yielding a score of 5.

Your method will take as input a int[] toss, where each element represents the upward face of a die, and return the maximum possible score with these values.

 

Definition

Class:YahtzeeScore

Method:maxPoints

Parameters:int[]

Returns:int

Method signature:int maxPoints(int[] toss)

(be sure your method is public)

    

 

Constraints

toss will contain exactly 5 elements.

Each element of toss will be between 1 and 6, inclusive.

 

Examples

0)

{ 2, 2, 3, 5, 4 }

Returns: 5

The example from the text.

 

1)

{ 6, 4, 1, 1, 3 }

Returns: 6

Selecting 1 as active yields 1 + 1 = 2, selecting 3 yields 3, selecting 4 yields 4 and selecting 6 yields 6, which is the maximum number of points.

 

2)

{ 5, 3, 5, 3, 3 }

Returns: 10

 

Source code

#include <iostream>

#include <vector>

using namespace std;

 

class YahtzeeScore

{

public:

      int maxPoints(vector<int> toss)

      {

           int count[6];

           memset(count, 0, sizeof(count));

           for(int i=0; i<5; i++)

           {

                 count[toss[i]-1]+=toss[i];

           }

           int max = 0;

           for(i=0; i<6; i++)

           {

                 if(count[i]>max)

                      max=count[i];

           }

           return max;

      }

};

 

void main()

{

      YahtzeeScore* ya = new YahtzeeScore;

      vector<int> v;

// test array: 2 2 3 5 4

//             6 4 1 1 3

//             5 3 5 3 3

      v.push_back(5);

      v.push_back(3);

      v.push_back(5);

      v.push_back(3);

      v.push_back(3);

      int ret = ya->maxPoints(v);

      cout<<ret<<endl;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值