SRM 212 Div II Level Two: WinningRecord,Brute Force
2013-07-06 21:06
561 查看
题目来源: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;
}
比较简单。
代码如下:
#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;
}
相关文章推荐
- SRM 212 Div II Level Two: WinningRecord,Brute Force
- SRM 212 Div II Level Three: LargestCircle,Brute Force
- SRM 578 Div II Level Two: GooseInZooDivTwo, DFS
- SRM 223 Div II Level Two: BlackAndRed,O(N)复杂度
- SRM 483 Div II Level Two: MovieSeating, TopCoder上正确率不到%10的题
- SRM 582 Div II Level Three: ColorTheCells, Brute Force 算法
- SRM 582 Div II Level Three: ColorTheCells, Brute Force 算法
- SRM 585 Div II Level Two: TrafficCongestionDivTwo
- SRM 577 Div II Level Two: EllysRoomAssignmentsDiv2
- SRM 219 Div II Level Two: HealthFood
- SRM 207 Div II Level Two: RegularSeason,字符串操作(sstream),多关键字排序( 比较函数 )
- SRM 577 Div II Level Two: EllysRoomAssignmentsDiv2
- SRM 223 Div II Level Two: BlackAndRed,O(N)复杂度
- SRM 233 Div II Level Two: PipeCuts
- SRM 579 Div II Level Two: UndoHistory
- SRM 299 Div II Level Two: Projections
- SRM 578 Div II Level Three: WolfInZooDivTwo,Dynamic Programming,求教!
- SRM 584 Div II Level Two: Egalitarianism,DFS,BFS算法
- SRM 212 Div II Level One: YahtzeeScore
- SRM 207 Div II Level Two: RegularSeason,字符串操作(sstream),多关键字排序(操作符重载)