Codeforces 1B. Spreadsheets

本文介绍了一种Excel坐标转换的方法,包括从Excel格式到(R,C)格式的转换,反之亦然。详细解析了转换算法,并提供了Java实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【题目来源】:点击打开链接

【题目描述】:

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
Copy
2
R23C55
BC23
output
Copy
BC23
R23C55

【题目分析】:此题大致意思为Excel的格式转换,Excel格式与(R,C)格式的转换类似于26进制与10进制的转换。

输入的有两种情况:①(R,C)格式如:R23C55,需要将其转换成字母+数字格式。R后面数字不用改变直接移到转换后的最后位置,将C后面的数字转换成26进制放到转换后的数字前完成转换。

Excel格式如BC23,需要将其转换成R+数字+C+数字格式。转换前的字母后面数字不用改变直接移到转换后的R后面的位置,将字母转换成10进制放到转换后C后面完成转换。

【AC代码】:

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		int n = s.nextInt();
		String a;
		char word[] = new char[27];
		word[1] = 'A';
		for (int i = 2; i <= 26; i++)
			word[i] = (char) (word[i - 1] + 1);
		for (int i = 0; i < n; i++) {
			a = s.next();
			int l = a.length();
			int flag = 0;
			for (int j = 0; j < l - 1; j++)
				/** 情况1:R23C55类型 */
				if (a.charAt(j) >= '0' && a.charAt(j) <= '9'
						&& a.charAt(j + 1) >= 'A' && a.charAt(j + 1) <= 'Z') {
					flag = 1;
				}
			if (flag == 1) {
				int n1 = 0, n2 = 0;
				int k = 1;
				/* 记录R后面数字 */
				while (a.charAt(k) >= '0' && a.charAt(k) <= '9') {
					n1 = n1 * 10 + a.charAt(k) - '0';
					k++;
				}
				/* 记录C后面数字 */
				for (k++; k < l; k++)
					n2 = n2 * 10 + a.charAt(k) - '0';
				int x[] = new int[10000];
				int t = 0;
				while (n2 > 0) {
					if (n2 % 26 == 0) {
						x[t++] = 26;
						n2 = n2 / 26 - 1;
					} else {
						x[t++] = n2 % 26;
						n2 /= 26;
					}
				}
				for (int j = t - 1; j >= 0; j--)
					System.out.print(word[x[j]]);
				System.out.println(n1);
			}
			/** 情况2:BC23类型 */
			else {
				int n1 = 0, n2 = 0;
				int k = 0;
				/*记录字母出现个数*/
				while (a.charAt(k) >= 'A' && a.charAt(k) <= 'Z') {
					n1 = n1 * 26 + a.charAt(k) - 'A' + 1;
					k++;
				}
				/*记录数字出现个数*/
				for (; k < l; k++)
					n2 = n2 * 10 + a.charAt(k) - '0';
				System.out.println("R" + n2 + "C" + n1);
			}
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值