百度面试题及答案
百度技术研发笔试题目
/*百度面试题
* 有一根27厘米的细木杆,在第3厘米、7厘米、11厘米、17厘米、23厘米这五个位置上各有一只蚂蚁。
* 木杆很细,不能同时通过一只蚂蚁。开始 时,蚂蚁的头朝左还是朝右是任意的,它们只会朝前走或调头,
* 但不会后退。当任意两只蚂蚁碰头时,两只蚂蚁会同时调头朝反方向走。假设蚂蚁们每秒钟可以走一厘米的距离。
*
*
* 分析:题目中的蚂蚁只可能相遇在整数点,不可以相遇在其它点,比如3.5cm处之类的,也就是可以让每只蚂蚁走 1秒,然后
* 查看是否有相遇的即可.
*
* 这样我的程序实现思路就是,初始化5只蚂蚁,让每只蚂蚁走1秒,然后看是否有相遇的,如果有则做相应处理.当每只蚂蚁都
* 走出木杆时,我就记录当前时间.这样就可以得到当前状态情况下,需要多久可以走出木杆,然后遍历所有状态则可以得到所胡
* 可能.
*/
package baidu;
public class Ant {
/*
* step 表示蚂蚁每一个单位时间所走的长度
*/
private final static int step = 1;
/*
* position表示蚂蚁所处的初始位置
*/
private int position;
/*
* direction表示蚂蚁的前进方向,如果为1表示向27厘米的方向走, 如果为-1,则表示往0的方向走。
*/
private int direction = 1;
/*
* 此函数运行一次,表示蚂蚁前进一个单位时间,如果已经走下木杆则会抛出异常
*/
public void walk() {
if (isOut()) {
throw new RuntimeException("the ant is out");
}
position = position + ction * step;
};
/**
* 检查蚂蚁是否已经走出木杆,如果走出返回true
*
*/
public boolean isOut() {
return position <= 0 || position >= 27;
}
/**
* 检查此蚂蚁是否已经遇到另外一只蚂蚁
* @param ant
* @return 如果遇到返回true
*/
public boolean isEncounter(Ant ant) {
return tion == tion;
}
/**
* 改变蚂蚁的前进方向
*/
public void changeDistation() {
direction = -1 * direction;
}
/**
* 构造函数,设置蚂蚁的初始前进方向,和初始位置
* @param position
* @param direction
*/
public Ant(int position, int direction) {
tion = position;
if (direction != 1) {
ction = -1;//方向设置初始位置,比如为0时,也将其设置为1.这样可以方便后面的处理
} else {
ction = 1;
}
}
}
/////////////////////////////////////////////////////////
package baidu;
public class Controller {
public static void main(String[] args) {
int time = 0;
for (int i = 0; i < 32; i++) {
Ant[] antArray = getAntList(getPoistions(), getDirections(i));
while (!isAllOut(antArray)) {
for (Ant ant : antArray) {
if (!t()) {
();
}
}
time++;
// 查看是否有已经相遇的Ant,如果有则更改其前进方向
dealEncounter(antArray);
}
tln(time);
// 将时间归0,这样可以重新设置条件,再次得到全部走完所需要的时间.
time = 0;
}
}
/**
* 这个函数的算法很乱,但暂时能解决问题
*
* @param list
*/
public static void dealEncounter(Ant[] antArray) {
int num_ant = th;
for (int j = 0; j < num_ant; j++) {
for (int k = j + 1; k < num_ant; k++) {
if (antArray[j]counter(antArray[k])) {
antArray[j]geDistation();
antArray[k]geDistation();
}
}
}
}
/**
* 因为有5只Ant,所以组合之后有32种组合.刚好用5位二进制来表示,如果为0则表示Ant往0的方向走 如果为1,则表示往27的方向走
*
* 注:在通过Ant的构造函数设置初始值时,通过过滤把0修改成了-1.
*/
public static int[] getDirections(int seed) {
int result[] = new int[5];
result[0] = seed % 2;
result[1] = seed / 2 % 2;
result[2] = seed / 4 % 2;
result[3] = seed / 8 % 2;
result[4] = seed / 16 % 2;
tln("directions is " + result[0] + "|" + result[1] + "|"
+ result[2] + "|" + result[3] + "|" + result[4]);
return result;
}
/**
* 批量设置Ant的初始位置,这样设置不是十分必要,可以直接在代码中设置
*
* @return
*/
public static int[] getPoistions() {
return new int[] { 3, 7, 11, 17, 23 };
}
/**
* 取得设置好初始值的5只Ant
*
* @param positions
* @param directions
* @return
*/
public static Ant[] getAntList(int[] positions, int[] directions) {
Ant ant3 = new Ant(positions[0], directions[0]);
Ant ant7 = new Ant(positions[1], directions[1]);
Ant ant11 = new Ant(positions[2], directions[2]);
Ant ant17 = new Ant(positions[3], directions[3]);
Ant ant23 = new Ant(positions[4], directions[4]);
return new Ant[] { ant3, ant7, ant11, ant17, ant23 };
}
/**
* 判断是否所有的Ant都已经走出了木杆,也就是设置退出条件
*
* @param antArray
* @return
*/
public static boolean isAllOut(Ant[] antArray) {
for (Ant ant : antArray) {
if (t() == false) {
return false;
}
}
return true;
}
}
编程:
用C语言实现一个revert函数,它的功能是将输入的字符串在原串上倒序后返回。
2 编程:
用C语言实现函数void * memmove(void *dest,const void *src,size_t n)。memmove
函数的功能是拷贝src所指的内存内容前n个字节
到dest所指的`地址上。
3 英文拼写纠错:
在用户输入英文单词时,经常发生错误,我们需要对其进行纠错。假设已经有一个包
含了正确英文单词的词典,请你设计一个拼写纠错
的程序。
(1)请描述你解决这个问题的思路;
(2)请给出主要的处理流程,算法,以及算法的复杂度;
(3)请描述可能的改进(改进的方向如效果,性能等等,这是一个开放问题)。
4 寻找热门查询:
搜索引擎会通过日志文件把用户每次检索使用的所有检索串都记录下来,每个查询串
的长度为1-255字节。假设目前有一千万个记录,
这些查询串的重复度比较高,虽然总数是1千万,但如果除去重复后,不超过3百万个
。一个查询串的重复度越高,说明查询它的用户越多,
也就是越热门。请你统计最热门的10个查询串,要求使用的内存不能超过1G。
(1)请描述你解决这个问题的思路;
(2)请给出主要的处理流程,算法,以及算法的复杂度。
5 集合合并:
给定一个字符串的集合,格式如:
{aaa bbb ccc}, {bbb ddd},{eee fff},{ggg},{ddd hhh}
要求将其中交集不为空的集合合并,要求合并完成后的集合之间无交集,例如上例应
输出
{aaa bbb ccc ddd hhh},{eee fff}, {ggg}
(1)请描述你解决这个问题的思路;
(2)请给出主要的处理流程,算法,以及算法的复杂度
(3)请描述可能的改进(改进的方向如效果,性能等等,这是一个开放问题)。
////////////////////////////////1
1 题
char *revert(char * str)
{
int n=strlen(str);
int i=0;
char c;
for(i=0;i
{
c=str;
str=str[n-i];
str[n-i]=c;
}
return str;
}
///////////////////////////////////
2 题
void * memmove(void *dest,const void *src,size_t n)
{
assert((dest!=0)&&(src!=0));
char * temp=(char * )dest;
char * ss=(char * )src;
int i=0;
for(;i
{
*temp++=*ss++;
}
return temp;
}
/////////////////////////////////////////////////
3 题
(1)思路 :
字典以字母键树组织,在用户输入同时匹配
(2)
流程:
每输入一个字母:
沿字典树向下一层,
a)若可以顺利下行,则继续至结束,给出结果;
b)若该处不能匹配,纠错处理,给出拼写建议,继续至a);
算法:
1.在字典中查找单词
字典采用27叉树组织,每个节点对应一个字母,查找就是一个字母
一个字母匹配.算法时间就是单词的长度k.
2.纠错算法
情况:当输入的最后一个字母不能匹配时就提示出错,简化出错处理,动态提示
可能 处理方法:
(a)当前字母前缺少了一个字母:搜索树上两层到当前的匹配作为建议;
(b)当前字母拼写错误:当前字母的键盘相邻作为提示;(只是简单的