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
- typedef uint64_t Type;
- typedef uint8_t Result[sizeof(Type) / 4];
- uint8_t toExcel(Type input, Result* result)
- {
- uint8_t position = 0;
- do
- {
- (*result)[position++] = input % 26;
- input = input / 26;
- }
- while (input > 0);
- return position;
- }
- void printResult(Type input, Result* result, uint8_t length)
- {
- std::cout << input << "\t";
- std::string string;
- for(uint8_t i = length; i > 0; --i)
- string += 'A' + (*result)[i - 1] - (i == 1 ? 0 : 1);
- std::cout << string;
- std::cout << std::endl;
- }
- int main(int, char*[])
- {
- uint64_t input;
- Result result;
- while (std::cin >> input)
- {
- if (input == 0)
- continue;
- uint8_t length = toExcel(input - 1, &result);
- printResult(input, &result, length);
- }
- 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 BA
- 12312 REN
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement