百度面試題及答案
百度技術研發筆試題目
/*百度面試題
* 有一根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 + this.direction * step;
};
/**
* 檢查螞蟻是否已經走出木杆,如果走出返回true
*
*/
public boolean isOut() {
return position <= 0 || position >= 27;
}
/**
* 檢查此螞蟻是否已經遇到另外一隻螞蟻
* @param ant
* @return 如果遇到返回true
*/
public boolean isEncounter(Ant ant) {
return ant.position == this.position;
}
/**
* 改變螞蟻的前進方向
*/
public void changeDistation() {
direction = -1 * direction;
}
/**
* 建構函式,設定螞蟻的初始前進方向,和初始位置
* @param position
* @param direction
*/
public Ant(int position, int direction) {
this.position = position;
if (direction != 1) {
this.direction = -1;//方向設定初始位置,比如為0時,也將其設定為1.這樣可以方便後面的.處理
} else {
this.direction = 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 (!ant.isOut()) {
ant.walk();
}
}
time++;
// 檢視是否有已經相遇的Ant,如果有則更改其前進方向
dealEncounter(antArray);
}
System.out.println(time);
// 將時間歸0,這樣可以重新設定條件,再次得到全部走完所需要的時間.
time = 0;
}
}
/**
* 這個函式的演算法很亂,但暫時能解決問題
*
* @param list
*/
public static void dealEncounter(Ant[] antArray) {
int num_ant = antArray.length;
for (int j = 0; j < num_ant; j++) {
for (int k = j + 1; k < num_ant; k++) {
if (antArray[j].isEncounter(antArray[k])) {
antArray[j].changeDistation();
antArray[k].changeDistation();
}
}
}
}
/**
* 因為有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;
System.out.println("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 (ant.isOut() == 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)當前字母拼寫錯誤:當前字母的鍵盤相鄰作為提示;(只是簡單的
【百度面試題及答案】相關文章: