分享
三行代码  ›  专栏  ›  技术社区  ›  Redowan K. Shajib

如何停止变量更新?

  •  -2
  • Redowan K. Shajib  · 技术社区  · 5 天前

    价值 count 每次循环(32-64-70等)持续增加。但我需要在完成每个循环后,值为零。

     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;
       }
    } 
    
        
    

    }

     // 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");
               
    

    }

    1 回复  |  直到 5 天前
        1
  •  0
  •   Barzana Panajotova    5 天前

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

    也可以使用类的方法手动重置它,例如重置计数()/设置计数(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);
    }