daily pastebin goal
0%
SHARE
TWEET

UnuDoi

a guest Feb 18th, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. char invert(char digit)
  4. {
  5.     if(digit == '1')
  6.         return '2';
  7.     else
  8.         return '1';
  9. }
  10.  
  11. char _getDigit(int index, int k, string base)
  12. {
  13.     if(k == 1)
  14.         return base[index - 1];
  15.     const int subSequenceLength = pow(4.0, k - 1);
  16.     int repetition = index / subSequenceLength ;
  17.     int innerI;
  18.     if(index % subSequenceLength == 0)
  19.         innerI = 4;
  20.     else
  21.     {
  22.         innerI = index % subSequenceLength;
  23.         repetition++;
  24.     }
  25.     char result = _getDigit(innerI, k - 1, base);
  26.     if(repetition == 2 || repetition == 3)
  27.         return invert(result);
  28.     return result;
  29. }
  30. char getDigit(int index, string base)
  31. {
  32.     int k = 1;
  33.     for(int i = 4; i < index; k++, i *= 4)
  34.         ;
  35.  
  36.     return _getDigit(index, k, base);
  37. }
  38.  
  39. int main()
  40. {
  41.     int digitIndex;
  42.     cin>>digitIndex;
  43.     cout<< getDigit(digitIndex, "1221")<< '\n';
  44.     return 0;
  45. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top