1B - Spreadsheet

#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;
}

 

转载于:https://www.cnblogs.com/brokenjar/p/1b-spreadsheet.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值