Codeforces Beta Round #1-B. Spreadsheets

这篇博客介绍了如何将Excel单元格坐标与RXCY格式互相转换的算法。作者通过分析两种坐标系统的特性,提出了判断输入格式的方法,并详细解释了26进制与十进制之间的转换过程。虽然问题看似简单,但作者在进制转换部分遇到了困难,最终通过查阅题解得以解决。博客以C++代码示例展示了转换过程,适合对编程和数据结构感兴趣的读者阅读。
摘要由CSDN通过智能技术生成

Codeforces Beta Round #1-B. Spreadsheets

In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.

The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23.

Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.

Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.

Input
The first line of the input contains integer number n (1 ≤ n ≤ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106 .

Output
Write n lines, each line should contain a cell coordinates in the other numeration system.

Examples
input

2
R23C55
BC23

output

BC23
R23C55

分析:题目大意就是,单元格坐标格式有两种(Excel和RXCY),输入某种格式,转换成另外一种格式。容易想到就是26进制与十进制之间的转换,不过1表示A、26表示Z,不能直接简单的转换。判断是哪一种格式也要注意细节。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main(){
    int n;
    cin>>n;
    while(n--){
        char a[50];
        cin>>a;
        int flag=0,c,b;
        for(int i=1;a[i]!='\0';i++){//判断是哪一种格式。
            if(a[i]=='C'&&i!=1&&!('A'<=a[i-1]&&a[i-1]<='Z')){
                flag=1;
                break;
            }
        }
        if(flag){
            int row=0,i;
            int col=0;
            char n[25];
            memset(n,0,sizeof(n));
            for(i=1;a[i]!='C';i++){
                row=row*10+(int)(a[i]-'0');
            }
            for(i=i+1;a[i]!='\0';i++){
                col=col*10+(int)(a[i]-'0');
            }
            i=0;
            while(col)//进制转换
            {
                int r=col%26;
                col=col/26;
                if(!r)//r=0无法表示,转化为26表示成‘Z’
                {
                    r=26;
                    col=col-1;
                    //例如AZ,col=1*26+26.r=0,下一个col=2.所以col要减去1。
                }
                n[i++]=(char)(r+'A'-1);
            }
            for(int i=strlen(n)-1;i>=0;i--){
                cout<<n[i];
            }
            cout<<row<<endl;
        }
        else{
            int row=0,i=0;
            int col=0;
            for(i=0;!('0'<=a[i]&&a[i]<='9');i++){
                col=col*26+(int)(a[i]-'A'+1);
            }
            for(i=i;a[i]!='\0';i++){
                row=row*10+(int)(a[i]-'0');
            }
            cout<<"R"<<row<<"C"<<col<<endl;
        }
    }
} 

也不知道为啥,会找这么久远的题目写,而且思路比较简单,不知道为啥要写个博客。其实进制转换那里我也不太懂,wa了好多次,最后还是看题解才解决,也不知道这样理解对不对。就当做一个记录吧!
”如切如磋,如琢如磨“

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值