现在的位置: 首页 > 综合 > 正文

SRM 212 Div II Level Two: WinningRecord,Brute Force

2013年10月01日 ⁄ 综合 ⁄ 共 824字 ⁄ 字号 评论关闭

题目来源: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;
}

抱歉!评论已关闭.