夢追い人

"It takes a dreamer to make a dream come true."―Vincent Willem van Gogh

SRM146 Div.2 Easy YahtzeeScore

健康診断の日なのに喉が痛いw

絶賛喉風邪なうです。

vectorをうまく使う。

さて。今回の問題文はコチラ

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)


CONSTRAINTS
  • toss will contain exactly 5 elements.
  • Each element of toss will be between 1 and 6, inclusive.

与えられた数列で同じ数字は足して、それぞれの合計が一番大きい合計を返す問題。

方針的には

  • ソート
  • その要素の数数える
  • その数字の最後の要素に回数分掛ける

とやりました。

class YahtzeeScore {
   public:
   int maxPoints(vector <int> toss)
  {
  	int count=0, tmp;
	sort(toss.begin(), toss.end());
	int i=0;
	while (i<=toss.size()) {
		if (count == 0) {
			tmp = toss[i];
			// cout << tmp << endl;
			count++; i++;
		} else {
			if (tmp==toss[i]) {
				count++;
				i++;
			} else {
				toss[i-1] *= count;
				// cout << toss[i-1] << endl;
				count=0;
			}
		}
	}
	sort(toss.begin(), toss.end());
	return toss[toss.size()-1]; 
  }
};

でも今気づいたんですがこんな事しなくても

int tmp = 0;
for (int i=1; i<toss.size(); i++) {
  if (toss[tmp] == toss[i]) {
    toss[tmp]+=toss[i];
  } else {
    tmp = i;
  }
}

で出来ますね(;´Д`)

まぁもういいけど…いいコードのほう残しておきましょう(笑)