成人福利av,电视剧全集在线观看免费高清,99热影院,视频一区二区精品,电影色戒未删完整版迅雷下载,激情欧美一区二区三区,亚洲热在线观看

百度技術(shù)研發(fā)類筆試題

時(shí)間:2024-07-29 07:15:54 學(xué)人智庫

百度技術(shù)研發(fā)類筆試題

  有一根27厘米的細(xì)木桿,在第3厘米、7厘米、11厘米、17厘米、23厘米這五個(gè)位置上各有一只螞蟻。木桿很細(xì),不能同時(shí)通過一只螞蟻。開始時(shí),螞蟻的頭朝左還是朝右是任意的,它們只會朝前走或調(diào)頭,但不會后退。當(dāng)任意兩只螞蟻碰頭時(shí),兩只螞蟻會同時(shí)調(diào)頭朝反方向走。假設(shè)螞蟻們每秒鐘可以走一厘米的距離。

百度技術(shù)研發(fā)類筆試題

  編寫程序,求所有螞蟻都離開木桿的最小時(shí)間和最大時(shí)間。

  分析:題目中的螞蟻只可能相遇在整數(shù)點(diǎn),不可以相遇在其它點(diǎn),比如3.5cm處之類的,也就是可以讓每只螞蟻?zhàn)?秒,然后查看是否有相遇的即可.這樣我的程序?qū)崿F(xiàn)思路就是,初始化5只螞蟻,讓每只螞蟻?zhàn)?秒,然后看是否有相遇的,如果有則做相應(yīng)處理.當(dāng)每只螞蟻都走出木桿時(shí),我就記錄當(dāng)前時(shí)間.這樣就可以得到當(dāng)前狀態(tài)情況下,需要多久可以走出木桿,然后遍歷所有狀態(tài)則可以得到所有可能.

  */

  packagebaidu;

  publicclassAnt{

  /*

  *step表示螞蟻每一個(gè)單位時(shí)間所走的長度

  */

  privatefinalstaticintstep=1;

  /*

  *position表示螞蟻所處的初始位置

  */

  privateintposition;

  /*

  *direction表示螞蟻的前進(jìn)方向,如果為1表示向27厘米的方向走,如果為-1,則表示往0的方向走。

  */

  privateintdirection=1;

  /*

  *此函數(shù)運(yùn)行一次,表示螞蟻前進(jìn)一個(gè)單位時(shí)間,如果已經(jīng)走下木桿則會拋出異常

  */

  publicvoidwalk(){

  if(isOut()){

  thrownewRuntimeException("theantisout");

  }

  position=position+this.direction*step;

  };

  /**

  *檢查螞蟻是否已經(jīng)走出木桿,如果走出返回true

  *

  */

  publicbooleanisOut(){

  returnposition<=0||position>=27;

  }

  /**

  *檢查此螞蟻是否已經(jīng)遇到另外一只螞蟻

  *@paramant

  *@return如果遇到返回true

  */

  publicbooleanisEncounter(Antant){

  returnant.position==this.position;

  }

  /**

  *改變螞蟻的前進(jìn)方向

  */

  publicvoidchangeDistation(){

  direction=-1*direction;

  }

  /**

  *構(gòu)造函數(shù),設(shè)置螞蟻的初始前進(jìn)方向,和初始位置

  *@paramposition

  *@paramdirection

  */

  publicAnt(intposition,intdirection){

  this.position=position;

  if(direction!=1){

  this.direction=-1;//方向設(shè)置初始位置,比如為0時(shí),也將其設(shè)置為1.這樣可以方便后面的處理

  }else{

  this.direction=1;

  }

  }

  }

  /////////////////////////////////////////////////////////

  packagebaidu;

  publicclassController{

  publicstaticvoidmain(String[]args){

  inttime=0;

  for(inti=0;i<32;i++){

  Ant[]antArray=getAntList(getPoistions(),getDirections(i));

  while(!isAllOut(antArray)){

  for(Antant:antArray){

  if(!ant.isOut()){

  ant.walk();

  }

  }

  time++;

  //查看是否有已經(jīng)相遇的Ant,如果有則更改其前進(jìn)方向

  dealEncounter(antArray);

  }

  System.out.println(time);

  //將時(shí)間歸0,這樣可以重新設(shè)置條件,再次得到全部走完所需要的時(shí)間.

  time=0;

  }

  }

  /**

  *這個(gè)函數(shù)的算法很亂,但暫時(shí)能解決問題

  *

  *@paramlist

  */

  publicstaticvoiddealEncounter(Ant[]antArray){

  intnum_ant=antArray.length;

  for(intj=0;j

  for(intk=j+1;k

  if(antArray[j].isEncounter(antArray[k])){

  antArray[j].changeDistation();

  antArray[k].changeDistation();

  }

  }

  }

  }

  /**

  *因?yàn)橛?只Ant,所以組合之后有32種組合.剛好用5位二進(jìn)制來表示,如果為0則表示Ant往0的方向走如果為1,則表示往27的方向走

  *

  *注:在通過Ant的構(gòu)造函數(shù)設(shè)置初始值時(shí),通過過濾把0修改成了-1.

  */

  publicstaticint[]getDirections(intseed){

  intresult[]=newint[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("directionsis"+result[0]+"|"+result[1]+"|"

  +result[2]+"|"+result[3]+"|"+result[4]);

  returnresult;

  }

  /**

  *批量設(shè)置Ant的初始位置,這樣設(shè)置不是十分必要,可以直接在代碼中設(shè)置

  *

  *@return

  */

  publicstaticint[]getPoistions(){

  returnnewint[]{3,7,11,17,23};

  }

  /**

  *取得設(shè)置好初始值的5只Ant

  *

  *@parampositions

  *@paramdirections

  *@return

  */

  publicstaticAnt[]getAntList(int[]positions,int[]directions){

  Antant3=newAnt(positions[0],directions[0]);

  Antant7=newAnt(positions[1],directions[1]);

  Antant11=newAnt(positions[2],directions[2]);

  Antant17=newAnt(positions[3],directions[3]);

  Antant23=newAnt(positions[4],directions[4]);

  returnnewAnt[]{ant3,ant7,ant11,ant17,ant23};

  }

  /**

  *判斷是否所有的Ant都已經(jīng)走出了木桿,也就是設(shè)置退出條件

  *

  *@paramantArray

  *@return

  */

  publicstaticbooleanisAllOut(Ant[]antArray){

  for(Antant:antArray){

  if(ant.isOut()==false){

  returnfalse;

  }

  }

  returntrue;

  }

  }

  編程:用C語言實(shí)現(xiàn)一個(gè)revert函數(shù),它的功能是將輸入的字符串在原串上倒序后返回。

【百度技術(shù)研發(fā)類筆試題】相關(guān)文章:

技術(shù)研發(fā)助理求職簡歷08-23

職稱英語試題綜合類模擬試題附答案07-08

研發(fā)技術(shù)員英文求職信06-11

2006年職稱英語考試綜合類(A類)試題及答案07-16

職稱英語綜合類閱讀模擬試題08-31

2006年職稱英語考試綜合類(B類)試題及答案08-05

山西中考信息技術(shù)試題(2)07-03

職稱英語綜合類考試題型06-12

職稱英語理工類C級試題及答案09-26

壓力危機(jī)類面試題目突破技巧11-09