fork download
  1. #include <stdio.h>
  2.  
  3. struct tuple{
  4. int x,y,z;
  5. };
  6.  
  7. int encode(int x,int y,int z){
  8. if (x==0 and y==0 and z==0) return 0;
  9. return (z%2) + 2 * encode(z/2,x,y);
  10. }
  11.  
  12. tuple decode(int n){
  13. tuple res={};
  14. if (not n) return res;
  15.  
  16. tuple got=decode(n/2);
  17. res.x+=got.y;
  18. res.y+=got.z;
  19. res.z+=got.x*2+(n%2);
  20. return res;
  21. }
  22.  
  23. int main(){
  24. tuple t={6,5,4};
  25.  
  26. int val=encode(t.x,t.y,t.z);
  27. printf("%d %d %d",t.x,t.y,t.z);
  28.  
  29. printf(" => %d => ",val);
  30. tuple res=decode(val);
  31. printf("%d %d %d\n",res.x,res.y,res.z);
  32.  
  33. return 0;
  34. }
Success #stdin #stdout 0s 2884KB
stdin
Standard input is empty
stdout
6 5 4 => 482 => 6 5 4