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了好多次,最后还是看题解才解决,也不知道这样理解对不对。就当做一个记录吧!
”如切如磋,如琢如磨“