题目来源: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. java_设计模式_外观模式_Facade Pattern(2016-08-09)

    外观模式/门面模式 1.概念 为子系统中的一组接口提供一个统一接口.Facade模式定义了一个高层接口,这个接口使得这子系统更容易使用. 2.UML 由于外观模式的结构图过于抽象,因此把它稍稍具体点. ...

  2. centos6.5安装vsftp服务并配置虚拟账户ftp

      当我们的用户量越来越大时,继续创建更多的系统用户是不明智的,这时就需要为vsftpd创建虚拟账户,但vsftpd虚拟账户的数据库要保存在Berkeley DB格式的数据文件中,所以需要安装db4- ...

  3. ZOJ3870 Team Formation

    /** Author: Oliver ProblemId: ZOJ3870 Team Formation */ /* 思路 1.异或运算,使用^会爆,想到二进制: 2.我们可以试着从前往后模拟一位一位 ...

  4. WCF服务对象实例化基础

    很多情况下,我们都需要控制wcf服务端对象的初始化方式,用来控制wcf实例在服务端的存活时间. Wcf框架提供了三种创建wcf实例的方式. WCF服务对象实例化基础 在普通的WCF请求和相应过程中,将 ...

  5. MyBatis Generator自动生成MyBatis的映射代码

    MyBatis Generator大大简化了MyBatis的数据库的代码编写,有了一个配置文件,就可以直接根据表映射成实体类.Dao类和xml映射.资源地址:MyBatis项目地址:http://my ...

  6. DOM对象控制HTML无素——详解1

    getElementsByName()方法 返回带有指定名称的节点对象的集合. 语法: document.getElementsByName(name) 与getElementById() 方法不同的 ...

  7. iOS适配:Masonry介绍与使用实践:快速上手Autolayout

    随着iPhone的手机版本越来越多, 那么对于我们广大的开发者来说就是很悲催,之前一直使用代码里面layout的约束来适配, 现在推荐一个第三方Masonry,上手块,操作简单,只能一个字形容他 “爽 ...

  8. PHP计划任务之关闭浏览器后仍然继续执行的函数 ignore_user_abort

    备忘一下这个函数: 函数名称:ignore_user_abort 本函数配置或取得使用端连接中断后,PHP 程序是否仍继续执行.默认值为中断连接后就停止执行.在 PHP 配置文件中 (php3.ini ...

  9. 项目任务管理(TaskMgr)设计篇

    为什么使用void FilllXX(TypeA pParm1, TypeB pParm2) 应用场景色:void FillXX的好处是可以不用关心实例情况:如果在方法体中需要一个实例,而方法体只知道基 ...

  10. 优化MYSQL FILESORT

    用Explain分析SQL语句的时候,经常发现有的语句在Extra列会出现Using filesort,根据mysql官方文档对他的描述: 引用 MySQL must do an extra pass ...