Spreadsheets_CF 1B

题目 题目链接

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.

Example
Input
2
R23C55
BC23
Output
BC23
R23C55


题意

将两种行列的表达式互转。

思路:

1. 模拟题
2. 对于任意输入,识别出表达式格式。并从字符串中获取出表达式的值,再转换到另一种表达式。

细节:

1.  如何区分两种格式。
2. A对应的是1,将字母转换成数字时需要注意。我采用的是A-Z分别映射为0-25。


代码

// CF 1BS
/*
	模拟
	string 类的应用
		find 构造 find_first_of
		string类转 int
			string::c_str()
			atoi  --- cstdlib
		int 转 string
			sstream   --- sstream
			sprintf   --- stdio
*/
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <cctype>
#include <algorithm>

using namespace std;

int main()
{
//#define LOCL
#ifdef LOCL
	freopen("data.in", "r", stdin);
//	freopen("data.out", "w+", stdout);
#endif // LOCL

    int n;
    string str;
    cin >> n;
    while (n--) {
		cin >> str;
		transform(str.begin(), str.end(), str.begin(), ::toupper);
		int pos_1, pos_2;
		pos_1 = pos_2 = 0;
		pos_1 = str.find('R');
		pos_2 = str.find('C', pos_1+1);
		if (pos_1 == 0 && pos_2 != string::npos && isdigit(str[pos_2-1])) {
		//RXCY to BC23
			pos_1 = str.find('R');
			pos_2 = str.find('C', pos_1+1);
			str[pos_2] = '\0';

			string s_r(&str[pos_1+1], &str[pos_2]);
			string s_c(&str[pos_2+1]);

			int c = atoi( s_c.c_str() );

			string cv;

			c--;
			int md;
			while ((md = c%26) != -1) {
				cv += md + 'A';
				c /= 26;
				c--;
			}
			reverse(cv.begin(), cv.end());

			cout << cv << s_r << endl;
		}
		else {
		//BC23 to RXCY
			int pos_1 = 0;
			pos_1 = str.find_first_of("0123456789");
			string s_c(&str[0], &str[pos_1]);
			string s_r(&str[pos_1]);

			long long num,pw;
			num = 0;
			pw = 1;
			transform(s_c.begin(), s_c.end(), s_c.begin(), ::toupper);

			for (int i = 1; i <= pos_1; i++) {
				int c = (s_c[pos_1-i] - 'A' + 1);
				num += c*pw;
				pw *= 26;
			}
			stringstream ss;
			ss << num;
			string cv;
			ss >> cv;
			cout << "R" << s_r << "C" << cv << endl;
		}
    }
    return 0;
}

补充:  substr --- 提取子字符串


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值