1B. Spreadsheets

一、Problem

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

三、Code

package com.codeforces.problemset;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class SpreadSheet {
    static char[] digits = {'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'};

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        StringBuilder result = new StringBuilder();
        String input;
        while (n-- > 0) {
            input = br.readLine();
            if (input.charAt(0) == 'R' && input.indexOf('C') >= 0 && Character.isDigit(input.charAt(input.indexOf('C') - 1))) {
                int cIndex = input.indexOf('C');
                int row = Integer.parseInt(input.substring(1, cIndex));
                int col = Integer.parseInt(input.substring(cIndex + 1));

                result.append(getRadix(col)).append(row);
            } else {
                int rowIndex = 0;
                while (!Character.isDigit(input.charAt(rowIndex))) {
                    rowIndex++;
                }

                String colStr = input.substring(0, rowIndex);
                int row = Integer.parseInt(input.substring(rowIndex));
                int col = 0;
                while (colStr.length() > 0) {
                    col = col * 26;
                    col = col + colStr.charAt(0) - 'A' + 1;
                    colStr = colStr.substring(1);
                }
                result.append("R").append(row).append("C").append(col);
            }
            result.append("\n");
        }
        System.out.println(result.toString());
    }

    private static String getRadix(int i) {
        StringBuilder sb = new StringBuilder();
        while (i > 0) {
            sb.insert(0, digits[(i - 1) % 26]);
            i = (i - 1) / 26;
        }
        return sb.toString().toUpperCase();
    }
}
发布了332 篇原创文章 · 获赞 198 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/riemann_/article/details/103324942
b1
今日推荐