SRM 207 Div II Level Two: RegularSeason,字符串操作(sstream),多关键字排序(操作符重载)
题目来源: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),多关键字排序(操作符重载)的更多相关文章
- SRM 577 Div II Level Two: EllysRoomAssignmentsDiv2
题目来源: http://community.topcoder.com/tc?module=ProblemDetail&rd=15497&pm=12521 这个问题要注意的就是只需要直 ...
- SRM 223 Div II Level Two: BlackAndRed,O(N)复杂度
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=3457&rd=5869 解答分析:http://comm ...
- SRM 582 Div II Level One: SemiPerfectSquare
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12580 比较简单,代码如下: #include <ios ...
- SRM 582 Div II Level Two SpaceWarDiv2
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12556 #include <iostream> # ...
- SRM 582 Div II Level Three: ColorTheCells, Brute Force 算法
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12581 Burte Force 算法,求解了所有了情况,注意 ...
- SRM 583 Div II Level One:SwappingDigits
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12609 #include <iostream> # ...
- SRM 583 Div II Level Three:GameOnABoard,Dijkstra最短路径算法
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12556 用Dijkstra实现,之前用Floyd算法写了一个, ...
- SRM 219 Div II Level One: WaiterTipping,小心约分
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12609&rd=15503 这题目看上去so easy, ...
- SRM 212 Div II Level One: YahtzeeScore
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=1692&rd=5858 比较简单. 代码如下: #inc ...
随机推荐
- hash表的建立和查找
(1)冲突处理方法为:顺次循环后移到下一个位置,寻找空位插入.(2)BKDE 字符串哈希unsigned int hash_BKDE(char *str){/* 初始种子seed 可取31 131 1 ...
- SGU 106.Index of super-prime
时间限制:0.25s 空间限制:4M 题目大意: 在从下标1开始素数表里,下标为素数的素数,称为超级素数(Super-prime),给出一个n(n<=10000) ...
- ios开发之代理&&协议(补充篇)
一.理解协议与代理 协议: 协议是一个方法签名的列表,在其中可以定义若干个方法.根据配置,遵守该协议的类会去实现这个协议中规定的若干个方法. 代理: 代理是一个概念,很难用一个名词去定义(如我们可以说 ...
- Bootstrap_Javascript_图片轮播
一 . 结构分析 一个轮播图片主要包括三个部分: ☑ 轮播的图片 ☑ 轮播图片的计数器 ☑ 轮播图片的控制器 第一步:设计轮播图片的容器.在 Bootstrap 框架中采用 carousel 样式,并 ...
- js获取url中的参数对象、js生成带参数的url
// 获取url中的参数,并返回一个对象 $.getRequestData = function() { var url = location.search; //获取url中"?" ...
- Linux之Vim编辑器使用
vim文本编辑器用于建立 编辑 显示文本文件,vim没有菜单,只有命令 在windows 平台下可使用gvim进行编写 Vim三种工作模式: 常有命令: 1.INSERT插入命令 i 在光标前插入 I ...
- 【技术宅6】把一个无限级分类循环成tree结构
function list_to_tree($list,$root=0,$pk='cid',$pid = 'pid',$child = '_child'){ if(is_array($list)) { ...
- ajax验证用户名和找回密码参考
// JavaScript Document function chkname(form){ var user = form.user.value; if(user == ''){ alert('请输 ...
- QQ空间API接口
(以下内容可能会随着时间改变而改变!) 查看对方QQ空间的背景音乐 http://qzone-music.qq.com/fcg-bin/cgi_playlist_xml.fcg?json=0& ...
- bzoj2127: happiness
Description 高一一班的座位表是个n*m的矩阵,经过一个学期的相处,每个同学和前后左右相邻的同学互相成为了好朋友.这学期要分文理科了,每个同学对于选择文科与理科有着自己的喜悦值,而一对好朋友 ...