流利说笔试编程题
编程题:第一道题目如下,当时没写出来。 第二道,是跳台阶(非递归)比较简单
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
struct ComeGoTime //用结构体为每个时间设置类型标记
{
int time;
bool type; // 时间类型: true 为进站时间 false 为出站时间
ComeGoTime(int tim,bool typ):time(tim),type(typ){}
};
bool cmp(ComeGoTime a,ComeGoTime b)
{
return a.time<b.time;
}
vector<int> getNum(string str) //由于输入没有 指定数据个数,所以以行字符串接受,再转换为数字
{
vector<int> vec;
if(str.length()<1)
return vec;
string::iterator first=str.begin();
int num=0;
int p=1;
for(;first!=str.end();first++)
{
if(*first==' ')
{
vec.push_back(num);
num=0;
p=1;
}
else
{
num*=p;
num+=(*first-'0');
p=10;
}
}
vec.push_back(num); //放入最后一个数
return vec;
}
int main()
{
string come_str,go_str;
getline(cin,come_str);
getline(cin,go_str);
vector<int> come_time,go_time;
come_time=getNum(come_str);
go_time=getNum(go_str);
vector<ComeGoTime> vec;
for(int i=0;i<come_time.size();i++)
{
ComeGoTime temp(come_time[i],true);
vec.push_back(temp);
}
for(int i=0;i<go_time.size();i++)
{
ComeGoTime temp(go_time[i],false);
vec.push_back(temp);
}
sort(vec.begin(),vec.end(),cmp); // 遇到进站加一,遇到出站减一,统计最大站台数量即可
int max_num=0;
int num=0;
for(int i=0;i<vec.size();i++)
{
if(vec[i].type==true)
num++;
else
num--;
max_num=max(max_num,num);
}
cout<<max_num<<endl;
return 0;
}
这是之后写的代码,样例过了,自己测了几组数据,通过。欢迎大家 的特殊数据的 检测
顺丰集团工作强度 274人发布