百度笔试题:绳子最多覆盖多少个点
2013-12-04 19:00
218 查看
版权所有。所有权利保留。
欢迎转载,转载时请注明出处:
http://blog.csdn.net/xiaofei_it/article/details/17123711
百度笔试题:
数轴上从左到右有n个点,a[0] ,a[1],…,a[n-1],给定一根长度为L绳子,求绳子最多覆盖其中几个点?
思路很清晰,直接上代码:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int n,l,a[1000];
while (cin>>n>>l)
{
for (int i=0;i<n;i++)
cin>>a[i];
sort(a,a+n);
int start=0,end=0,max=1;
while (true)
{
while (end<n&&a[end]-a[start]<=l)
end++;
if (end-start>max)
max=end-start;
if (end==n)
break;
end--;
do
{
start++;
}while (start<n&&a[end]-a[start]>=l);
if (start==n)
break;
}
cout<<max<<endl;
}
return 0;
}
欢迎转载,转载时请注明出处:
http://blog.csdn.net/xiaofei_it/article/details/17123711
百度笔试题:
数轴上从左到右有n个点,a[0] ,a[1],…,a[n-1],给定一根长度为L绳子,求绳子最多覆盖其中几个点?
思路很清晰,直接上代码:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int n,l,a[1000];
while (cin>>n>>l)
{
for (int i=0;i<n;i++)
cin>>a[i];
sort(a,a+n);
int start=0,end=0,max=1;
while (true)
{
while (end<n&&a[end]-a[start]<=l)
end++;
if (end-start>max)
max=end-start;
if (end==n)
break;
end--;
do
{
start++;
}while (start<n&&a[end]-a[start]>=l);
if (start==n)
break;
}
cout<<max<<endl;
}
return 0;
}
相关文章推荐
- 一个关于if else容易迷惑的问题
- 房产界已上市和IPO路上的难兄难弟:房天下与房多多
- 利用百度地图提供的API做的应用
- 搜狗百度360市值齐跌:搜索引擎们陷入集体焦虑?
- 百度20年:搜索帝国的崛起、式微与重生
- 动易2006序列号破解算法公布
- 百度全面恢复网站权重,流量暴涨中
- 一道sql面试题附答案
- C#数据结构与算法揭秘二
- C# 超高面试题收集整理
- 浅析STL中的常用算法
- JavaScript 组件之旅(二)编码实现和算法
- 百度 popup.js 完美修正版非常的不错 脚本之家推荐
- 人人网javascript面试题 可以提前实现下
- 百度空间的popup效果分析第1/3页
- 模仿百度三维地图的js数据分享
- 百度最近不收站分析,为什么不收录你的站
- 百度手写板代码JavaScript远程调用的实现(鼠标输入法)
- 据说是雅虎的一份PHP面试题附答案
- php牛逼的面试题分享