Advertisement
TheHardew

Codeforces - 1B

Dec 6th, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.40 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <cstdio> // cstdio is faster, right? I don't know if I should mix it with iostream
  3. #include <cctype>
  4. #include <algorithm>
  5. #include <iostream>
  6. #include <string>
  7.  
  8. std::string charToNum(const std::string cell);
  9. std::string numToChar(const std::string cell);
  10. std::string convert(const std::string cell);
  11. unsigned uPow(unsigned base, unsigned exponent);
  12.  
  13. int main() {
  14.     unsigned n;
  15.     std::scanf("%u", &n);
  16.     std::getchar();
  17.  
  18.     std::string input;
  19.     for (unsigned i = 0; i < n; ++i) {
  20.         std::getline(std::cin, input);
  21.        
  22.         std::printf("%s\n", convert(input).c_str());
  23.     }
  24. }
  25.  
  26. std::string charToNum(const std::string cell) {
  27.     std::string columnsStr;
  28.  
  29.     // we'll proccess letters to a number later, first we need to extract columns and rows
  30.     unsigned i = 0; // we'll need that after the loop
  31.     while (std::isalpha(cell[i])) // string "cell" for sure ends with a number, so we're not gonna go past it
  32.         columnsStr += cell[i++];
  33.  
  34.     unsigned rows, columns = 0;
  35.     std::sscanf(cell.c_str() + i, "%u", &rows);
  36.  
  37.     //convert columns from letters to the number
  38.     for (size_t i = 0, j = columnsStr.size(); i < columnsStr.size(); ++i, --j)
  39.         columns += uPow(26, j - 1) * (columnsStr[i] - 'A' + 1);
  40.  
  41.     return {'R' + std::to_string(rows) + 'C' + std::to_string(columns)};
  42. }
  43.  
  44. std::string numToChar(const std::string cell) {
  45.     unsigned rows, columns;
  46.     std::sscanf(cell.c_str(), "R%uC%u", &rows, &columns);
  47.  
  48.     // convert columns from the number to letters
  49.     std::string columnsStr;
  50.     for (size_t i = columns; i; i = (i - 1) / 26)
  51.         columnsStr += (i % 26 != 0) ? 'A' + i % 26 - 1 : 'Z'; // if 26 | i we have a Z
  52.  
  53.     // letters are readen ltr, text is rtl
  54.     std::reverse(columnsStr.begin(), columnsStr.end());
  55.  
  56.     return {columnsStr + std::to_string(rows)};
  57. }
  58.  
  59. std::string convert(const std::string cell) {
  60.     bool lettersTwice = true; // set it to true, so that first letter in string doesn't trigger if statement
  61.    
  62.     // if last char isn't a letter and this one is, we've got letters twice
  63.     for (unsigned i = 0; i < cell.size(); ++i)
  64.         if (!lettersTwice && std::isalpha(cell[i])) {
  65.             lettersTwice = true;
  66.             break;
  67.         }
  68.         else
  69.             lettersTwice = std::isalpha(cell[i]);
  70.  
  71.     if (lettersTwice)
  72.         return numToChar(cell);
  73.     else
  74.         return charToNum(cell);
  75. }
  76.  
  77. unsigned uPow(unsigned base, unsigned exponent) {
  78.     unsigned ret = 1;
  79.     for (unsigned i = 0; i < exponent; ++i)
  80.         ret *= base;
  81.     return ret;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement