1B - Spreadsheet
2013-10-14 21:44 BrokenJar 阅读(126) 评论(0) 编辑 收藏 举报#include <algorithm> #include <sstream> #include <iostream> #include <string> #include <cmath> #include <cctype> #include <cstdlib> #include <cstring> int main() { int n; std::cin >> n; for(int i = 0; i < n ; ++i) { std::string sin; std::ostringstream outs; std::cin >> sin; std::string::iterator iter = sin.end(); if(sin[0] == 'R' && std::isdigit(sin[1])) iter = std::find(sin.begin(), sin.end(), 'C'); if(iter == sin.end())//BC23->R23C55 { std::string::iterator pos; pos = std::find_if(sin.begin(), sin.end(), isdigit); int col = 0; for(iter = sin.begin(); iter != pos; ++iter) col += (*iter - 'A' + 1) * std::ceil(std::pow(26, std::distance(iter, pos) - 1)); outs << 'R' << std::string(pos, sin.end()) << 'C' << col; } else//R23C55->BC23 { std::string::size_type pos; pos = sin.find('C'); std::string scol; int col = std::atoi(sin.substr(pos + 1, sin.length()).c_str()); while(col > 0) { char alpha = static_cast<char>('A' + col % 26 - 1); col /= 26; if (!std::isalpha (alpha)) { alpha = 'Z'; --col; } scol.push_back(alpha); } std::reverse(scol.begin(), scol.end()); outs << scol << sin.substr(1, pos - 1); } std::cout << outs.str() << std::endl; } return EXIT_SUCCESS; }