Advertisement
zamotivator

Untitled

Dec 29th, 2014
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 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. uint8_t result_length = toExcel(input, &result);
  51. std::string result_string;
  52. while(result_length > 0)
  53. {
  54. result_string += 'A' + result[--result_length];
  55. }
  56. std::cout << input << "\t" << result_string << std::endl;
  57. }
  58. return 0;
  59. }
  60. otsarev@tsarev-desktop:~ ─────────────────────────────────────────────────────────────────────────────────
  61. ➜ g++ ./test.cpp --std=c++11
  62. otsarev@tsarev-desktop:~ ─────────────────────────────────────────────────────────────────────────────────
  63. ➜ echo "0 1 2 3 25 26 27 28 51 52 53 12312" | ./a.out
  64. 0 A
  65. 1 B
  66. 2 C
  67. 3 D
  68. 25 Z
  69. 26 AA
  70. 27 AB
  71. 28 AC
  72. 51 AZ
  73. 52 AA
  74. 53 AB
  75. 12312 FAO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement