Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ➜ cat test.cpp
- #include <iostream>
- #include <cstdint>
- // Is translation from 2-position to 26-position notation
- //
- // X - bit count in source
- // Y - position count in result
- //
- // sizeof(T) = log(base=2, number=X)
- // X = power(2, sizeof(T))
- // Y = log(base=26, number=X)
- // Y = log(base=26 number=power(2, sizeof(T)))
- // Y = sizeof(T) * log(base=26, number=2)
- //
- // python:
- // >>> math.log(2, 26)
- // 0.21274605355336315
- //
- // Y ~ = sizeof(T) / 4
- template<typename T>
- struct Size
- {
- enum { value = sizeof(T) / 4 };
- };
- template<typename T>
- uint8_t toExcel(T input, uint8_t (*result)[Size<T>::value])
- {
- uint8_t position = 0;
- do
- {
- *result[position++] = input % 26;
- input = input / 26;
- }
- while (input > 0);
- return position;
- }
- int main(int, char*[])
- {
- uint64_t input;
- uint8_t result[Size<uint64_t>::value];
- while (std::cin >> input)
- {
- if (input == 0)
- continue;
- uint8_t result_length = toExcel(input-1, &result);
- std::string result_string;
- while(result_length > 0)
- {
- result_string += 'A' + result[--result_length];
- }
- std::cout << input << "\t" << result_string << std::endl;
- }
- return 0;
- }
- otsarev@tsarev-desktop:~ ─────────────────────────────────────────────────────────────────────────────────
- ➜ g++ ./test.cpp --std=c++11
- otsarev@tsarev-desktop:~ ─────────────────────────────────────────────────────────────────────────────────
- ➜ echo "0 1 2 3 25 26 27 28 51 52 53 12312" | ./a.out
- 1 A
- 2 B
- 3 C
- 25 Y
- 26 Z
- 27 AA
- 28 AB
- 51 AY
- 52 AZ
- 53 AA
- 12312 FAN
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement