Advertisement
zamotivator

Untitled

Dec 29th, 2014
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 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. typedef uint64_t Type;
  24. typedef uint8_t Result[sizeof(Type) / 4];
  25.  
  26. uint8_t toExcel(Type input, Result* result)
  27. {
  28. uint8_t position = 0;
  29. do
  30. {
  31. (*result)[position++] = input % 26;
  32. input = input / 26;
  33. }
  34. while (input > 0);
  35. return position;
  36. }
  37.  
  38.  
  39. void printResult(Type input, Result* result, uint8_t length)
  40. {
  41. std::cout << input << "\t";
  42. std::string string;
  43. for(uint8_t i = length; i > 0; --i)
  44. string += 'A' + (*result)[i - 1] - (i == 1 ? 0 : 1);
  45. std::cout << string;
  46. std::cout << std::endl;
  47. }
  48.  
  49.  
  50. int main(int, char*[])
  51. {
  52. uint64_t input;
  53. Result result;
  54. while (std::cin >> input)
  55. {
  56. if (input == 0)
  57. continue;
  58. uint8_t length = toExcel(input - 1, &result);
  59. printResult(input, &result, length);
  60. }
  61. return 0;
  62. }
  63. otsarev@tsarev-desktop:~ ─────────────────────────────────────────────────────────────────────────────────
  64. ➜ g++ ./test.cpp --std=c++11
  65. otsarev@tsarev-desktop:~ ─────────────────────────────────────────────────────────────────────────────────
  66. ➜ echo "0 1 2 3 25 26 27 28 51 52 53 12312" | ./a.out
  67. 1 A
  68. 2 B
  69. 3 C
  70. 25 Y
  71. 26 Z
  72. 27 AA
  73. 28 AB
  74. 51 AY
  75. 52 AZ
  76. 53 BA
  77. 12312 REN
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement