我有顺序搜索的问题,这是练习:
您正在测试一种砖的物理耐力。测试是为了找到砖块可以在不破坏和N层建筑的情况下落下的最大高度。你有k砖和两个案例:
案例1.当k = 1时,尝试从当前楼层向上依次开始,直到砖块断裂。
案例2.对于k>从地板N / 2上掉下一块砖。如果它破裂,则在楼层1和N / 2 -1之间再次应用案例2。如果砖没有破裂,则在楼层N / 2 + 1和N之间再次应用案例2。
我对CASE 1的原因有疑问,因为我不知道如何处理它:
最初你调用dropBricks(k,1,N);由用户(键盘)引入k和N.
public static boolean breakingFloor(int floor){
boolean breaks;
Random r =new Random();
int breakingFloor= r.nextInt(floor)+1;
if(floor>=breakingFloor){
breaks=true;
}else{
breaks=false;
}
return breaks;
}
dropBricks收到k(剩余砖块的数量,第一层(第一层),最后一层(最后一层= N)),输出将是破碎地板
public static int dropBricks(int k,int first,int last){
int result=0;
if (k==1){//CASE 1
//I KNOW THIS CODE IS WRONG, IS JUST O NOT LEAVE IT IN EMPTY
do{
dropBricks(k,first,last);
first++;
}while(breakingFloor(first));
System.out.println("Floor brick broke "+floor);
}else{//CASE 2
if(last-first<=1){
if(last==first){
result=first;
System.out.println("Floor brick broke "+result);
}else{
if(breakingFloor(first)){
result=first;
k--;
System.out.println("Floor brick broke "+result);
}else{
result=last;
k--;
System.out.println("Floor brick broke "+result);
}
}
}else{
int mid=((first+last)/2);
if(breakingFloor(mid)){
result=dropBricks(k-1,first,mid);
System.out.println("Floor brick broke "+result);
}else{
result=dropBricks(k,mid,last);
System.out.println("Floor brick broke "+result);
}
}
}
return result;
}
答案 0 :(得分:2)
你的问题 - 或者至少其中一个 - 就是每次你绕过这个循环......
do{
dropBricks(k,first,last);
first++;
} while( breakingFloor(first) );
...您正在选择一个新的随机区域,它将在其中断(在breakingFloor
方法中发生):
Random r =new Random();
int breakingFloor= r.nextInt(floor)+1;
这肯定会给你非常不一致的行为。它可能实际上有些时候是侥幸。
您想要选择在开始时会破坏的楼层,并保持不变。
测试应用程序的一种明智方法可能是硬编码破土动工,然后再引入随机性。
答案 1 :(得分:2)
您的第一个问题在这里:
<div id="show-more" class="ghost"></div>
<div class="show-hide-text">
<a class="show-less" href="#show-less">Show less</a>
<a class="show-more" href="#show-more">Show more</a>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
</div>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
这根本没有意义。砖块断裂的楼层编号不随机。
换句话说:您想为该砖分配一个一次的随机数。然后你的算法必须找到那个数字!您的代码会更改&#34;破坏楼层编号&#34; 而您的算法会尝试查找该号码。这使得整个过程毫无用处(当然,&#34;随机&#34 ;;因为各种各样的事情都可能发生)。
所以迈出第一步:退一步,重新思考问题,解决问题&#34;问题真的要求你这样做。