Advertisement
zamotivator

Untitled

Dec 29th, 2014
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. ➜ cat test.cpp
  2. #include <iostream>
  3. #include <cstdint>
  4.  
  5.  
  6. // Is translation from 2-position to 26-position notation
  7. //
  8. // X - bit count in source
  9. // Y - position count in result
  10. //
  11. // sizeof(T) = log(base=2, number=X)
  12. // X = power(2, sizeof(T))
  13. // Y = log(base=26, number=X)
  14. // Y = log(base=26 number=power(2, sizeof(T)))
  15. // Y = sizeof(T) * log(base=26, number=2)
  16. //
  17. // python:
  18. // >>> math.log(2, 26)
  19. // 0.21274605355336315
  20. //
  21. // Y ~ = sizeof(T) / 4
  22.  
  23.  
  24. template<typename T>
  25. struct Size
  26. {
  27. enum { value = sizeof(T) / 4 };
  28. };
  29.  
  30. template<typename T>
  31. uint8_t toExcel(T input, uint8_t (*result)[Size<T>::value])
  32. {
  33. uint8_t position = 0;
  34. do
  35. {
  36. *result[position++] = input % 26;
  37. input = input / 26;
  38. }
  39. while (input > 0);
  40. return position;
  41. }
  42.  
  43.  
  44. int main(int, char*[])
  45. {
  46. uint64_t input;
  47. uint8_t result[Size<uint64_t>::value];
  48. while (std::cin >> input)
  49. {
  50. if (input == 0)
  51. continue;
  52. uint8_t result_length = toExcel(input-1, &result);
  53. std::string result_string;
  54. while(result_length > 0)
  55. {
  56. result_string += 'A' + result[--result_length];
  57. }
  58. std::cout << input << "\t" << result_string << std::endl;
  59. }
  60. return 0;
  61. }
  62. otsarev@tsarev-desktop:~ ─────────────────────────────────────────────────────────────────────────────────
  63. ➜ g++ ./test.cpp --std=c++11
  64. otsarev@tsarev-desktop:~ ─────────────────────────────────────────────────────────────────────────────────
  65. ➜ echo "0 1 2 3 25 26 27 28 51 52 53 12312" | ./a.out
  66. 1 A
  67. 2 B
  68. 3 C
  69. 25 Y
  70. 26 Z
  71. 27 AA
  72. 28 AB
  73. 51 AY
  74. 52 AZ
  75. 53 AA
  76. 12312 FAN
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement