Advertisement
Guest User

hill climbing

a guest
Jul 22nd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5.     int global_max=-1,global_index;
  6.  
  7.  
  8.     int hill[10]= {1,9,0,3,5,2,9,7,100,1};
  9.  
  10.     for(int i=0; i<100; i++)   //global search for 100 times
  11.     {
  12.         int local_max=-1,local_index=-1;
  13.  
  14.             int x= rand()%10; //random index selected 10 times
  15.  
  16.             for(int j=x; j<10; j++)   //local max finding
  17.             {
  18.                 if(hill[j]>=local_max)
  19.                 {
  20.                     local_max=hill[j];
  21.                     local_index=j;
  22.                 }
  23.                 else break;
  24.             }
  25.  
  26.             if(  global_max<=local_max)
  27.             {
  28.                 global_max=local_max;
  29.                 global_index=local_index;
  30.             }
  31.  
  32.     }
  33.     cout<<"global maximum : "<<global_max<<endl;
  34.     cout<<"index of global maximum : "<<global_index<<endl;
  35.  
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement