Untitled

 avatar
unknown
c_cpp
4 years ago
917 B
9
Indexable
   struct idx {
       int j;
       int i;
   };
   
   struct idx find_idx(int **arr, int n, int base_i, int base_j, int idx) {
       if (n == 2) {
           const struct idx rets[4] = { {0,0}, {0,1}, {1,1}, {1,0}};
           struct idx ret = rets[idx];
           ret.j += base_j;
           ret.i += base_i;
           return ret;
       }
   
       int qtr = floor(((float)idx / (n * n)) * 4);
       if (qtr == 1 || qtr == 2)
           base_i += n / 2;
       if (qtr == 2 || qtr == 3)
           base_j += n / 2;
   
       return find_idx(arr, n / 2, base_i, base_j, idx % ((n * n) / 4));
   }

   int main (int argc ,char **argv) {
       int arr[][4] = {
           {0,1,4,5},
           {3,2,7,6},
           {0xc,0xd,8,9},
           {0xf, 0xe, 0xb, 0xa},
       };
   
       int idx = atoi(argv[1]);
       struct idx ret = find_idx(arr, 4, 0, 0, idx);
       printf("%d %d\n", ret.j, ret.i);
   }
Editor is loading...