题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=3003&rd=5858


比较简单。

代码如下:

 

#include <iostream>
#include <vector>
#include <cmath>
#include <string>

using namespace std;

#define Rate(win, i) ( (double)(win) / (double)(i) * 100)
#define PRECISE 0.000001

class WinningRecord
{
public:
	vector <int> getBestAndWorst(string games);
};

vector <int> WinningRecord::getBestAndWorst(string games)
{
	int i;
	int best, worst, win;
	double bestRate, worstRate, rate;
	int length;
	vector <int> ans;

	best = worst = win = 0;
	length = games.size();
	bestRate =  0;
	worstRate = 100;

	for (i = 0; i < 2; i++) {
		if ('W' == games[i]) {
			++win;
		}
	}

	for (i = 2; i < length; i++) {
		if ('W' == games[i]) {
			++win;
		}

		rate = Rate(win, i+1);
		if ( rate - bestRate > PRECISE || abs(bestRate - rate) < PRECISE ) {
			bestRate = rate;
			best = i + 1;
		}

		if ( worstRate - rate > PRECISE || abs(worstRate - rate) < PRECISE ) {
			worstRate = rate;
			worst = i + 1;
		}
	}

	ans.push_back(best);
	ans.push_back(worst);
	return ans;
}


 

 

相关文章:

  • 2021-11-20
  • 2021-04-03
  • 2021-10-15
  • 2021-08-06
  • 2021-12-17
  • 2021-12-13
猜你喜欢
  • 2021-06-22
  • 2021-07-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-03
  • 2021-10-06
相关资源
相似解决方案