SHARE
TWEET

Untitled

a guest Feb 17th, 2017 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<iostream>
  2. #include<math.h>
  3. #include<vector>
  4. using namespace std;
  5. int main(){
  6.  
  7.     long long int correct;
  8.     vector<int> path;
  9.     long long int time=1;
  10.     int n=0, i=0;
  11.     bool flag=true;
  12.     cin>>correct;
  13.     while(flag==true&&correct!=1){
  14.         if((correct-1)<pow(10,i)){n=i; flag=false;}
  15.         i++;
  16.     }
  17.     if(correct==1)n=1;
  18.     long long int max=pow(10,n);
  19.     long long int min=0;
  20.     long long int guess=max/2;
  21.     path.push_back(guess);
  22.     //looping
  23.     while(guess!=correct&&min!=correct){
  24.         //cout<<"debug: max | guess | min "<<max<<"|"<<guess<<"|"<<min<<endl;
  25.         if(correct>guess){
  26.             min=guess;
  27.             if((guess+max)%2==0)guess=(max+guess)/2;
  28.             else if((guess+max)%2==1)guess=(max+1+guess)/2;
  29.         }
  30.         else if(correct<guess){
  31.             max=guess;
  32.             if((guess+min)%2==0)guess=(min+guess)/2;
  33.             else if((guess+min)%2==1)guess=(min+1+guess)/2;
  34.         }
  35.         path.push_back(guess);
  36.         time++;
  37.     }
  38.     //print result
  39.     if(time!=1)cout<<time<<" times : ";
  40.     else if(time==1)cout<<time<<" time : ";
  41.     for(int i=0;i<path.size();i++){
  42.         cout<<path[i];
  43.         if(i!=path.size()-1)cout<<"->";
  44.     }
  45.     return 0;
  46. }
RAW Paste Data
Top