codeforces - 1B - Spreadsheets

Spreadsheets
time limit per test
10 seconds
memory limit per test
64 megabytes
input
standard input
output
standard output

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

题意:

将R23C55(第23行55列)转换成另一种表达形式BC23,即将行用字母表示,A到Z(26位),下一位为AA(27),再例如BZ(2*26+26=78)。

以及将形如BC23转换成R23C55。


思路:

模拟一发即可。要注意判断到底是Excel转换成RXCY还是RXCY转换成Excel,不可以单纯的判断首字母为R,第二个字母为数字,就断定为是将RXCY转换成Excel格式。


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>

using namespace std;

const int MAXN = 1e6 + 10;
int len;
char str[MAXN];

void one(){
	int pos, a=0, b=0;
	for(int i=0; i<len; ++i){
		if(str[i]=='C') {
			pos = i;
			break;
		}
	}
	for(int i=1; i<pos; ++i){
		a = a*10+(str[i]-'0');
	}
	for(int i=pos+1; i<len; ++i){
		b = b*10+(str[i]-'0');
	}
	stack<char> s;
	while(b>0){
	    if(b%26==0){
            s.push('Z');
            b -= 26;
	    }
        else
            s.push('A'-1+b%26);
        b/=26;
	}
	while(!s.empty()){
        printf("%c", s.top());
        s.pop();
	}
	printf("%d\n", a);
}

void two(){
	int pos;
	for(int i=0; i<len; ++i){
		if(isdigit(str[i])){
			pos = i; break;
		}
	}
	int num = 0;
	for(int i=0; i<pos-1; ++i){
		num = num*26 + (str[i]-'A'+1)*26;
	}
	num += str[pos-1]-'A';
	printf("R");
	for(int i=pos; i<len; ++i) printf("%c", str[i]);
	printf("C%d\n", num+1);
}

int main(){
	int n;
	scanf("%d", &n);
	while(n--){
        getchar();
		scanf("%s", str);
		len = strlen(str);
		bool aa = false;
		bool bb = false;
		for(int i=0; i<len; ++i){
            if(isdigit(str[i])) aa = true;
            if(aa && isalpha(str[i])){
                bb = true; break;
            }
		}
		if(bb)
			one();
		else
			two();
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值