Codeforces Beta Round #1 B Spreadsheets

来源:互联网 发布:anaconda python 使用 编辑:程序博客网 时间:2024/05/16 09:00

B. 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 follown lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than106 .

Output

Write n lines, each line should contain a cell coordinates in the other numeration system.

Sample test(s)
Input
2R23C55BC23
Output
BC23
R23C55


意理解:
将字母设为列,数字设为行,每个点的坐标有两种表现形式,要求输入一种形式将其另外一种形式输出。
:R23C55代表行23列55  则其另外一种形式为BC23,BC代表第55列。
      如果是BC23,即这种形式的字符串则代表前面是列,后面的数字代表行。输出为R23C55
 解题思路:
     该题主要是两个点
     1.判断输入的字符串为哪种形式。
     2.如何实现字符和数字之间的转换。
 code:
#include <iostream>#include <cstdio>#include <algorithm>#include <string.h>#include <cmath>using namespace std;char a[1000005];char ch[30];int final[1005];int main(){    int n,i,j;    int row,col;    scanf("%d",&n);    while(n--)    {        int flag = 0;        row =0;        col = 0;        getchar();        scanf("%s",a);        int v;        for(int i = 2; i < strlen(a); ++i)            if(a[0] == 'R' && a[1] - '0' < 10 && (a[i] == 'C'))            {                flag = 1;   //代表输入的字符串为R23C55格式                v = i;     //记录列的第一个数字的位置                break;            }        if(flag)        {                row = a[1] - '0';            for(i = 2;i<strlen(a)-1;i++)            {                if(a[i] >= '0'&&a[i]<='9')                {                    row=row*10+a[i]-'0';                }                else                    break;            }            col = a[v+1]-'0';            for( j = v+2;j < strlen(a);j++)            {                if(a[j]>='0'&&a[j]<='9')                {                    col=col*10 + a[j] - '0';                }                else                    break;            }            int now = 0;    //将列拆为字母表示            while(col > 0)            {                final[now++] = ((col-1) % 26) + 'A';                   col = (col-1)/ 26;            }            for(int i = now - 1; i  >= 0; i--)                putchar(final[i]);            printf("%d\n",row);        }        else    //输入的字符串为BC23格式        {            row = 0;            col = 0;            for(int i = 0;i < strlen(a);i++)            {                if(a[i]-'0'<10)                {                    row = row*10 + a[i]-'0';                     }                else                {                    col = col*26 + a[i]-'A'+1;   //将字母转换成数字。                }            }            printf("R%dC%d\n",row,col);        }    }    return 0;}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

字母数字之间转换的思路

我们假设字母为 An An-1 An-2...A1。那么这个表示的数字与AAA...A(n个A)的距离为

            dis = (An - 'A')*26^(n- 1)+ (An-1 - 'A' )*26^(n-2)..+(A1-'A')*1,

把这个数加上AAA...A(n个A)即我们要求的数。

            nA = 26^(n-1)+26^(n-2)...+26
            number = (An - 'A' + 1)*26^(n- 1)+ (An-1 - 'A' + 1)*26^(n-2)..+(A1 - 'A' + 1)*1。  

那么要把数字转为字母序列也很简单即:A1 = (number - 1) % 26 + 'A'以此类推。

0 0