Codeforces 1B. 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
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);
			}
		}
	}
}

猜你喜欢

转载自blog.csdn.net/mcp3128/article/details/80636570
今日推荐