百度技术研发类笔试题

时间:2024-07-29 07:15:54 学人智库 我要投稿
  • 相关推荐

百度技术研发类笔试题

  有一根27厘米的细木杆,在第3厘米、7厘米、11厘米、17厘米、23厘米这五个位置上各有一只蚂蚁。木杆很细,不能同时通过一只蚂蚁。开始时,蚂蚁的头朝左还是朝右是任意的,它们只会朝前走或调头,但不会后退。当任意两只蚂蚁碰头时,两只蚂蚁会同时调头朝反方向走。假设蚂蚁们每秒钟可以走一厘米的距离。

百度技术研发类笔试题

  编写程序,求所有蚂蚁都离开木杆的最小时间和最大时间。

  分析:题目中的蚂蚁只可能相遇在整数点,不可以相遇在其它点,比如3.5cm处之类的,也就是可以让每只蚂蚁走1秒,然后查看是否有相遇的即可.这样我的程序实现思路就是,初始化5只蚂蚁,让每只蚂蚁走1秒,然后看是否有相遇的,如果有则做相应处理.当每只蚂蚁都走出木杆时,我就记录当前时间.这样就可以得到当前状态情况下,需要多久可以走出木杆,然后遍历所有状态则可以得到所有可能.

  */

  packagebaidu;

  publicclassAnt{

  /*

  *step表示蚂蚁每一个单位时间所走的长度

  */

  privatefinalstaticintstep=1;

  /*

  *position表示蚂蚁所处的初始位置

  */

  privateintposition;

  /*

  *direction表示蚂蚁的前进方向,如果为1表示向27厘米的方向走,如果为-1,则表示往0的方向走。

  */

  privateintdirection=1;

  /*

  *此函数运行一次,表示蚂蚁前进一个单位时间,如果已经走下木杆则会抛出异常

  */

  publicvoidwalk(){

  if(isOut()){

  thrownewRuntimeException("theantisout");

  }

  position=position+this.direction*step;

  };

  /**

  *检查蚂蚁是否已经走出木杆,如果走出返回true

  *

  */

  publicbooleanisOut(){

  returnposition<=0||position>=27;

  }

  /**

  *检查此蚂蚁是否已经遇到另外一只蚂蚁

  *@paramant

  *@return如果遇到返回true

  */

  publicbooleanisEncounter(Antant){

  returnant.position==this.position;

  }

  /**

  *改变蚂蚁的前进方向

  */

  publicvoidchangeDistation(){

  direction=-1*direction;

  }

  /**

  *构造函数,设置蚂蚁的初始前进方向,和初始位置

  *@paramposition

  *@paramdirection

  */

  publicAnt(intposition,intdirection){

  this.position=position;

  if(direction!=1){

  this.direction=-1;//方向设置初始位置,比如为0时,也将其设置为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++;

  //查看是否有已经相遇的Ant,如果有则更改其前进方向

  dealEncounter(antArray);

  }

  System.out.println(time);

  //将时间归0,这样可以重新设置条件,再次得到全部走完所需要的时间.

  time=0;

  }

  }

  /**

  *这个函数的算法很乱,但暂时能解决问题

  *

  *@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();

  }

  }

  }

  }

  /**

  *因为有5只Ant,所以组合之后有32种组合.刚好用5位二进制来表示,如果为0则表示Ant往0的方向走如果为1,则表示往27的方向走

  *

  *注:在通过Ant的构造函数设置初始值时,通过过滤把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;

  }

  /**

  *批量设置Ant的初始位置,这样设置不是十分必要,可以直接在代码中设置

  *

  *@return

  */

  publicstaticint[]getPoistions(){

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

  }

  /**

  *取得设置好初始值的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都已经走出了木杆,也就是设置退出条件

  *

  *@paramantArray

  *@return

  */

  publicstaticbooleanisAllOut(Ant[]antArray){

  for(Antant:antArray){

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

  returnfalse;

  }

  }

  returntrue;

  }

  }

  编程:用C语言实现一个revert函数,它的功能是将输入的字符串在原串上倒序后返回。

【百度技术研发类笔试题】相关文章:

技术研发助理求职简历08-23

职称英语试题综合类模拟试题附答案07-08

研发技术员英文求职信06-11

2006年职称英语考试综合类(A类)试题及答案07-16

职称英语综合类阅读模拟试题08-31

2006年职称英语考试综合类(B类)试题及答案08-05

山西中考信息技术试题(2)07-03

职称英语综合类考试题型06-12

职称英语理工类C级试题及答案09-26

压力危机类面试题目突破技巧11-09