首页 > 解决方案 > 如何阻止变量更新?

问题描述

的值count随着每个循环(32-64-70 等)不断增加。但我需要在完成每个循环后将该值设为零。

公共类 BinarySearchLecture {

 int count = 0;

int binarySearchLecture(int arr[], int l, int r, int x) {
   if (r >= l) { 
      int mid = l + (r - l) / 2; 
      count++;
      if (arr[mid] == x) 
      {
         count++;
         return mid; 
      }
      if (arr[mid] > x) 
      {  
         count++; 
         return binarySearchLecture(arr, l, mid - 1, x);
      }
      else  
      {  
          count++;
         return binarySearchLecture(arr, mid + 1, r, x); 
      }
   } else {     
      return -1;
   }
} 

    

}

导入 java.util.Random;公共类 FirstAssignment {

 // Driver method to test  
public static void main(String args[]) 
{ 
    BinarySearchLecture bs = new BinarySearchLecture(); 
    int arr[] = new int[65536];
    int n = arr.length; 
    
     
    for (int i = 0; i<n; i++) 
    {
         arr[i] = new Random().nextInt(10000000);
    }
    

    
    for (int i = 1; i <=3; i++)
    {

         int x = new Random().nextInt(10000000);
        System.out.println( i + "." + "Random search key: " + x );
    


    int result = bs.binarySearchLecture(arr, 0, n - 1, x); 
    if (result == -1) 
        System.out.println("Element not present"); 
    else 
        System.out.println("Element found at index " + result); 
    
         int count = bs.count; // count is an class variable
          
          System.out.println("Number of comparisons are " + count); 
    
     
        //  System.out.format("%32s%10d%16s", x , count , "presnt");
           

} }

标签: java

解决方案


您的 binarySearchLecture 方法正在增加计数值。

您也可以使用 bs.resetCount()/bs.setCount(0) 之类的方法或类似的方法手动重置它。您可以将其添加到您定义的 bs 类中。

public class BinarySearchLecture {

int count = 0;
 int binarySearchLecture(int arr[], int l, int r, int x) {
   if (r >= l) { 
      int mid = l + (r - l) / 2; 
      count++;
      if (arr[mid] == x) 
      {
         count++;
         return mid; 
      }
      if (arr[mid] > x) 
      {  
         count++; 
         return binarySearchLecture(arr, l, mid - 1, x);
      }
      else  
      {  
          count++;
         return binarySearchLecture(arr, mid + 1, r, x); 
      }
   } else {     
      return -1;
   }
  }

void resetCount(){
  count = 0;
}
}

然后你的代码看起来更像这样:

 for (int i = 1; i <=3; i++)
{

     int x = new Random().nextInt(10000000);
    System.out.println( i + "." + "Random search key: " + x );



int result = bs.binarySearchLecture(arr, 0, n - 1, x); 
if (result == -1) 
    System.out.println("Element not present"); 
else 
    System.out.println("Element found at index " + result); 

     int count = bs.count; // count is an class variable
      
      System.out.println("Number of comparisons are " + count); 

 
    //  System.out.format("%32s%10d%16s", x , count , "presnt");
     
   bs.resetCount();
}

另一个不高效的解决方案 - 每个循环创建一个 bs 类的新实例。

for (int i = 1; i <= 3; i++) {
  int x = new Random().nextInt(10000000);
  System.out.println(i + "." + "Random search key: " + x);

  bs = new BinarySearchLecture();

  int result = bs.binarySearchLecture(arr, 0, n - 1, x);
  if (result == -1){
    System.out.println("Element not present");
  }
  else {
    System.out.println("Element found at index " + result);
  }
    int count = bs.count; // count is an class variable

    System.out.println("Number of comparisons are " + count);
}

推荐阅读