您的位置:首页 > 其它

CodeForces 1B Spreadsheets(模拟)

2016-07-21 16:17 399 查看
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表格),我们经常用大写的字母来表示列,例如A表示第1列,B表示第2列,第26列用Z来表示,同时第27列我们用AA来表示,第28列我们用AB来表示,第29列我们用AC来表示,AZ表示第52列,ZZ之后我们就需要用3个字母来表示列了。行的表示比较简单,我们一般用正整数来表示,比如1就表示第1行,5就表示第5行,行和列一起表示成为类似BC23的情况,这个表示在第23行,第55列。有时候,我们的系统也会用RXCY的格式来表示,X和Y是整数,分别表示行号和列号。例如:R23C55表示的位置和之前那个例子是一样的。
        你的任务是写一个程序,将这些位置在两种不同的表示方法之间转化。

        第一行是一个正整数n(1<=n<=10^5), 表示测试数据的数量。接下来n行,每行一串字符,表示一个位置,输入保证所有的位置都是正确的,没有一个位置的行号或者列号超过10^ 6。
解题思路:
        分成两种情况处理,第一种情况将10进制转换为26进制,第二种情况将26进制转换成10进制,注意保存中间结果的数组开大一点,因为这个原因RE了几次(WA了无数次,参考别人的代码,终于过了。。。)
代码如下:
#include <cstdio>
#include <cstring>
#include <cctype>
#include <stack>
#include <string>

using namespace std;
char letter[27] = {'0','A','B','C','D','E','F','G','H','I','J','K','L','M',
'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
int r,c;
char str[100005];

void switch1()
{
r = 0,c = 0;
int len = strlen(str);
int i;
for(i = 1;str[i] != 'C';i++){
r = str[i] - '0' + r * 10;
}
for(i = i + 1;i < len;i++){
c = str[i] - '0' + c * 10;
}
int temp[10005];
i = 0;
while(c){
if(c % 26 == 0){
temp[i++] = 26;
c = c / 26 - 1;
}else{
temp[i++] = c % 26;
c /= 26;
}
}
int j;
for(j = i - 1;j >= 0;j--){
printf("%c",letter[temp[j]]);
}
printf("%d\n",r);
}

void switch2()
{
r = 0,c = 0;
int len = strlen(str);
int i;
for(i = 0;i < len;i++){
if(!isupper(str[i]))
break;
else{
c = (str[i] - 'A' + 1) + c * 26;
}
}
for(;i < len;i++){
r = str[i] - '0' + r * 10;
}
printf("R%dC%d\n",r,c);
}

int main()
{
int n,i,j,k;
int flag;
scanf("%d",&n);
while(n--){
flag = 0;
scanf("%s",str);

for(i = 1; str[i] != '\0';i++){
if(str[i] >= '0' && str[i] <= '9' && str[i + 1] >= 'A' && str[i + 1] <= 'Z'){
flag = 1;
}
}

if(flag == 1)
switch1();
else
switch2();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  CodeForces