fork download
  1. #include <stdio.h>
  2. //convert (x,y) to d
  3. int xy2d (int n, int x, int y) {
  4. int rx, ry, s, d=0;
  5. for (s=n/2; s>0; s/=2) {
  6. rx = (x & s) > 0;
  7. ry = (y & s) > 0;
  8. d += s * s * ((3 * rx) ^ ry);
  9. rot(s, &x, &y, rx, ry);
  10. }
  11. return d;
  12. }
  13.  
  14. //convert d to (x,y)
  15. void d2xy(int n, int d, int *x, int *y) {
  16. int rx, ry, s, t=d;
  17. *x = *y = 0;
  18. for (s=1; s<n; s*=2) {
  19. rx = 1 & (t/2);
  20. ry = 1 & (t ^ rx);
  21. rot(s, x, y, rx, ry);
  22. *x += s * rx;
  23. *y += s * ry;
  24. t /= 4;
  25. }
  26. }
  27.  
  28. //rotate/flip a quadrant appropriately
  29. void rot(int n, int *x, int *y, int rx, int ry) {
  30. if (ry == 0) {
  31. if (rx == 1) {
  32. *x = n-1 - *x;
  33. *y = n-1 - *y;
  34. }
  35.  
  36. //Swap x and y
  37. int t = *x;
  38. *x = *y;
  39. *y = t;
  40. }
  41. }
  42. int main(void) {
  43. int x = 0;
  44. int y = 0;
  45. d2xy(1000, 228, &x, &y);
  46. printf("%d %d", x,y);
  47. }
  48.  
Runtime error #stdin #stdout 0s 2292KB
stdin
asd
stdout
9 3