SRM 207 Div II Level Two: RegularSeason,字符串操作(sstream),多关键字排序( 比较函数 )
来源:互联网 发布:时时彩辅助软件 编辑:程序博客网 时间:2024/05/22 02:01
题目来源: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 cmp (const Team &a, const Team &b){if ( a.wins != b.wins ) {return a.wins > b.wins;/* 按 wins */} else {return a.name < b.name;/* 按 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(), cmp);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 582 Div II Level Two SpaceWarDiv2
- SRM 219 Div II Level Two: HealthFood
- SRM 233 Div II Level Two: PipeCuts
- SRM 299 Div II Level Two: Projections
- SRM 577 Div II Level Two: EllysRoomAssignmentsDiv2
- SRM 585 Div II Level Two: TrafficCongestionDivTwo
- SRM 579 Div II Level Two: UndoHistory
- SRM 212 Div II Level Two: WinningRecord,Brute Force
- SRM 584 Div II Level Two: Egalitarianism,DFS,BFS算法
- SRM 222 Div II Level Two: GroceryBagger,STL map 用法
- SRM 223 Div II Level Two: BlackAndRed,O(N)复杂度
- SRM 578 Div II Level Two: GooseInZooDivTwo, DFS
- SRM 207 Div II Level One: TransportCounting
- SRM 207 Div II Level Three: CaptureThemAll,BFS算法
- SRM 583 Div Level Two:IDNumberVerification
- SRM 483 Div II Level Two: MovieSeating, TopCoder上正确率不到%10的题
- SRM 585 Div II Level Three: EnclosingTriangleColorful
- CentOS SSH配置
- 输入法服务的生命周期
- 步步辨析JS中的对象成员
- 多平台工程生成工具CMAKE如何创建VS工程组——folder属性
- struts 1和struts2 区别
- SRM 207 Div II Level Two: RegularSeason,字符串操作(sstream),多关键字排序( 比较函数 )
- 五十个UI设计资源网站
- SWT, Jface和 RCP
- MySQL select into 和 SQL select into
- 从曲线拟合问题窥视机器学习中的相关概念
- oracle基础学习之profile 概要文件
- CF-46D Parking lot
- *.aps,*.ncb.*.opt文件分别用来做什么(转)
- Jquery操作select、form表单