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

主要是要对字符串的操作要熟悉,熟练使用 sstream 流可以大大简化操作,如这个题目,如果不用 sstream 流的话,用 sscanf 函数非常麻烦,因为输入的数据中数字的个数不是一样的,还有一个问题就是多关键字的排序,用 sort 函数时要自己写比较函数。

另外那个得到实现四则运算的方法也很巧妙,我刚始用的方法比较麻烦,这种方法看别人代码知道的。

代码如下:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream> using namespace std; class RegularSeason
{
public:
vector <string> finalStandings(vector <string> teams, int rounds);
}; struct Team {
int wins;
string name;
}; /* 比较函数 */
bool operator< (const Team &a, const Team &b)
{
if ( a.wins != b.wins ) {
return a.wins > b.wins; /* 按 wins */
} else {
return a.name < b.name; /* 按 name */
}
}; vector <string> RegularSeason::finalStandings(vector <string> teams, int rounds)
{
int num = teams.size();
int prob;
vector <Team> vt(num);
vector <string> ans; for (int i = 0; i < num; i++) {
vt[i].wins = 0;
} for (int i = 0; i < num; i++) {
istringstream iss(teams[i]);
iss >> vt[i].name;
for (int j = 0; j < num; j++) {
iss >> prob; /* 得到羸的可能值 */
if (i != j) { /* 更新期望值 */
vt[j].wins += rounds * (100 - prob);
vt[i].wins += rounds * prob;
}
}
} sort(vt.begin(), vt.end());
for (int i = 0; i < num; i++) {
ostringstream oss;
int iwins = vt[i].wins;
string teamname = vt[i].name; /* 四舍5入约分 */
iwins = (iwins + 50) / 100; oss << teamname << " " << iwins; ans.push_back(oss.str());
} return ans;
}

SRM 207 Div II Level Two: RegularSeason,字符串操作(sstream),多关键字排序(操作符重载)的更多相关文章

  1. SRM 577 Div II Level Two: EllysRoomAssignmentsDiv2

    题目来源: http://community.topcoder.com/tc?module=ProblemDetail&rd=15497&pm=12521 这个问题要注意的就是只需要直 ...

  2. SRM 223 Div II Level Two: BlackAndRed,O(N)复杂度

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=3457&rd=5869 解答分析:http://comm ...

  3. SRM 582 Div II Level One: SemiPerfectSquare

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12580 比较简单,代码如下: #include <ios ...

  4. SRM 582 Div II Level Two SpaceWarDiv2

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12556 #include <iostream> # ...

  5. SRM 582 Div II Level Three: ColorTheCells, Brute Force 算法

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12581 Burte Force 算法,求解了所有了情况,注意  ...

  6. SRM 583 Div II Level One:SwappingDigits

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12609 #include <iostream> # ...

  7. SRM 583 Div II Level Three:GameOnABoard,Dijkstra最短路径算法

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12556 用Dijkstra实现,之前用Floyd算法写了一个, ...

  8. SRM 219 Div II Level One: WaiterTipping,小心约分

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12609&rd=15503 这题目看上去so easy, ...

  9. SRM 212 Div II Level One: YahtzeeScore

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=1692&rd=5858 比较简单. 代码如下: #inc ...

随机推荐

  1. jQuery慢慢啃之选择器(二)

    1.$("#myDiv");ID匹配一个元素 <span id="foo[bar]"></span> $("#foo\\[ba ...

  2. Fedora 21 安装VirtualBox

    注: 所有操作需要root权限  如果不是root用户在下面所有命令前加sudo 装dkms,kernel-devel,makecache: yum install dkms yum install  ...

  3. 算法系列之图--DFS

    深度优先搜索使用的策略是,只要与可能就在图中尽量“深入”.DFS总是对最近才发现的结点v出发边进行探索,知道该结点的所有出发边都被发现为止.一旦v的所有出发边都被发现了,搜索就回溯到v的前驱结点(v是 ...

  4. ICE学习第一步-----配置ICE环境变量

    安装 ICE: 1.下载ICE: http://www.zeroc.com/download.html 下载说明:ICE支持语言(C++, Java, C#, Visual Basic,Python, ...

  5. 【开源】前端练手笔记,Chrome扩展应用程序(html+CSS+JS) (1)

    项目名称:github-notification 项目地址:https://github.com/WQTeam/github-notification 说明:本人打算抽时间学习前端(html + cs ...

  6. redis运行状态图形化监控工具 — RedisLive

    在Centos中部署redis运行状态图形化监控工具 — RedisLive   写在前面 前两天看到张善友老师的一篇文章<先定个小目标, 使用C# 开发的千万级应用>,里面给出了一张腾讯 ...

  7. iOS应用开发:什么是ARC?

    iOS应用开发:什么是ARC? 博客分类: Phone / IOS / Objective-C / Swift   ARC是什么 ARC是iOS 5推出的新功能,全称叫 ARC(Automatic R ...

  8. 一定要记住这20种PS技术!!!会让你的照片美的不行!!!

    一种简单的数码照片后期润饰 1 打开图片,执行色像/饱和度(-40)降低饱和度. 2 新建一图层,将图层模式改为柔光,用画笔工具将需要润饰的部分画几下,这里可以利用色板方便的提取颜色 3 图片色彩过渡 ...

  9. gcc链接g++编译生成的静态库和动态库的makefile示例

    使用c++开发程序或者库时,将库提供给其他人使用. 然而使用者是使用c开发的程序,链接g++编译生成的库时,于链接gcc生成的库,有所不同. 首先是静态库,以链接g++编译生成的libmylib.a为 ...

  10. 【Java】WSDL 简介

    WSDL(网络服务描述语言,Web Services Description Language)是一门基于 XML 的语言,用于描述 Web Services 以及如何对它们进行访问. 什么是 WSD ...