每晚都会被一道sb题卡到自闭
人品指数
时间限制: 1 Sec 内存限制: 128 MB
[提交] [状态]
题目描述
队员们都到齐了,大家先入住宾馆,由于扬州是著名的旅游城市,宾馆房间很难预订,节目组预订的房间有高级的和普通的。怎样分配房间成了一个很棘手的问题,技术控李晨提出用“人品指数”来决定房间的好坏,根据往期的节目中的表现来计算人品,比如撕掉别人的名牌的人加10分,玩某个游戏得第一名加5分,第二名加3分等等。当然也可以减分,比如没有及时救助队友减10分,玩某个游戏超时减5分等等。不过,一次扣分和加分的数值不会超过100。
计算每个队员的人品指数时,每人一行。一开始的时候会给每个队员的人品指数设为100。比如李晨的信息为:lichen:100-3-5+1+2-2,则他的人品指数为:93。
到底谁能得到宾馆的最好房间呢?
输入
输入有若干行(不超过100行)。每行为一个队员的信息,其中首先是一个姓名(不超过20个字符的字符串,只含有小写字母和空格),后面是一个冒号,再后面为类似于数学的加减式(其中没有多余空格,保证合法)表示一个队员的人品加减分情况。
输出
输出人品指数最高的队员名单。如果有多个队员的人品指数一样高,请按姓名的字典顺序全部输出(一行一个姓名)。
样例输入 Copy
xiaoy:100-3-5+1+2-2
xiaox:100-10-20+1
xiaoz:100-50-50-1
样例输出 Copy
xiaoy
提示
对于100%的数据,队员的信息不超过100行。
重点有几个:
1.如何读入数据?
题目中已经提示名字中可能含有空格,所以采用getline读入
2.如何提取出名字?
string有一个函数是find
int pos=s.find(':');
表示 :的位置
3.如何计算出每个人的分数?
模拟就好,洛谷上有个类似的题:传送门
4.如何将答案保存并输出?
不知道为什么非得用结构体才能A
vector它不香吗 哎
#include<bits/stdc++.h> using namespace std; const int maxn=1100; struct node{ string name; int score; }ans[maxn]; int tot; bool cmp(node a,node b){ if(a.score==b.score) return a.name<b.name; return a.score>b.score; } void AC(){ string s; int maxx=-1; vector<string>v; while(getline(cin,s)){ int pos=s.find(':'); string name=s.substr(0,pos); string cul=s.substr(pos+1,s.size()-pos); char flag='+';int res=0; for(int i=0;i<cul.size();i++) if(cul[i]>='0'&&cul[i]<='9') tmp=tmp*10+(cul[i]-'0'); else{ if(flag=='+') res+=tmp; else res-=tmp; tmp=0; flag=cul[i]; } if(flag=='+') res+=tmp; else res-=tmp; ///cout<<name<<" "<<res<<endl; /* if(maxx==res) v.push_back(name); else if(maxx<res){ v.clear(); v.push_back(name); maxx=res; } */ ans[++tot].score=res; ans[tot].name=name; } sort(ans+1,ans+1+tot,cmp); ///sort(v.begin(),v.end()); /// for(auto tt:v) cout<<tt<<endl; for(int i=1;i<=tot;i++) if(ans[i].score==ans[1].score) cout<<ans[i].name<<endl; } int main(){ AC(); return 0; }
话说我为什么要写这篇题解