70055

PAT1006

这道题对我来说还是有些难度的,难度在于我对字符串处理的方法不熟悉,字符串的识别,切割,截取

在这道题里面我是使用string的find(要查找的字符,起始位置)和substr(起始位置,切割长度)方法,个人记法,切割长度就是要截下来的串的末尾位置-起始位置+1

同时,在获取输入时候使用cin.get(c)来搞掉换行符(或者其他你不想要的符号)

#include<iostream> #include<string> #include<vector> #include<algorithm> using namespace std; int min_compare(vector<string> &in){ vector<int> candidate; vector<int> time; for(int i=0;i!=in.size();++i){ candidate.push_back(i); } while(true){ time.clear(); for(int i=0;i!=candidate.size();++i){ int number=candidate[i]; int posi=in[number].find(':',0); if(posi!=-1){ string t=in[number].substr(0,posi); time.push_back((t[0]-'0')*10+(t[1]-'0')); in[number]=in[number].substr(posi+1,in[number].size()-posi-1); } else{ string t=in[number].substr(0,in[number].size()); time.push_back((t[0]-'0')*10+(t[1]-'0')); } } int min_time=*min_element(time.begin(),time.end()); int count=0; for(int i=0;i!=time.size();++i){ if(time[i]==min_time){ candidate[count]=candidate[i]; ++count; } } if(count==1) return candidate[0]; } } int max_compare(vector<string> &out){ vector<int> candidate; vector<int> time; for(int i=0;i!=out.size();++i){ candidate.push_back(i); } while(true){ time.clear(); for(int i=0;i!=candidate.size();++i){ int number=candidate[i]; int posi=out[number].find(':',0); if(posi!=-1){ string t=out[number].substr(0,posi); time.push_back((t[0]-'0')*10+(t[1]-'0')); out[number]=out[number].substr(posi+1,out[number].size()-posi-1); } else{ string t=out[number].substr(0,out[number].size()); time.push_back((t[0]-'0')*10+(t[1]-'0')); } } int max_time=*max_element(time.begin(),time.end()); int count=0; for(int i=0;i!=time.size();++i){ if(time[i]==max_time){ candidate[count]=candidate[i]; ++count; } } if(count==1) return candidate[0]; } } int main(){ vector<string> id; vector<string> in; vector<string> out; int num; cin>>num; //discard the line break char c; cin.get(c); for(int i=0;i!=num;i++){ string line; getline(cin,line); string::size_type posi1=line.find(" ",0); id.push_back(line.substr(0,posi1)); string::size_type posi2=line.find(" ",posi1+1); in.push_back(line.substr(posi1+1,posi2-posi1-1)); out.push_back(line.substr(posi2+1,line.size()-posi2-1)); } int sign_in=min_compare(in); int sign_out=max_compare(out); cout<<id[sign_in]<<" "<<id[sign_out]; return 0; }

  

来源:博客园

作者:川川——叮叮咚咚

链接:https://www.cnblogs.com/chuan-chuan/p/11424588.html

Recommend