codeforces--Spreadsheets(模拟)

来源:互联网 发布:oracle数据库安装 编辑:程序博客网 时间:2024/06/05 14:42

Description

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.

Sample Input
Input

2
R23C55
BC23

Output

BC23
R23C55

思路:
此题如果单纯地用普通进制数来想的话,那就大错特错了。因为比如:
26进制其位上的数字对应为0-25,而此题为1-26;也就是说没有0!!!
那么当计算时需要向前进位时,应当减去其低一位的能够存储的最大值。
就像52,对于普通26进制其结果应当为20,而此题为1 26!!!也就是进位的时候,向前进的实际值为总值减去低位能够存储的最大值!!!每一位都是要记录大于零的值!!!(没有错,我错了很多遍~~~)

#include <cstdio>#include <cstring>#include <set>#include <queue>#include <map>#include <iostream>#include <string>#include <algorithm>#include <vector>#include <cstdlib>using namespace std;int main(){    int m,i;    string s;    char s1[1000009];    cin>>m;    while(m--)    {        cin>>s;        int num = 0;        for(i=0; i<s.size()-1; i++)        {            if(s[i]<='Z'&&s[i]>='A'&&s[i+1]>='0'&&s[i+1]<='9')                num++;        }        if(num == 1)        {            int r = 0,c = 0;            for(i=0; i<s.size(); i++)            {                if(s[i]>='0'&&s[i]<='9')                {                    r = r*10 + s[i] - '0';                }                if(s[i]<='Z'&&s[i]>='A')                {                    c = c*26 + s[i] - 'A'+1;                }            }            cout<<"R"<<r<<"C"<<c<<endl;        }        else        {            int flag = 1,r = 0;            for(i=1; i<s.size(); i++)            {                if(s[i]=='C')                {                    flag = i;                    break;                }            }            for(i=flag + 1; i<s.size(); i++)            {                r = r*10 + s[i] - '0';            }            int k =0;            while(r)            {                if(r%26==0)//特判!!!                {                    s1[k++] = 'Z';                    r=r/26-1;//要减一哦哦哦哦哦哦!!!!                }                else                {                    s1[k++] = 'A' + r%26-1;                    r/=26;                }            }            for(i=k-1; i>=0; i--)                cout<<s1[i];            for(i=1; i<flag; i++)                cout<<s[i];            cout<<endl;        }    }    return 0;}
原创粉丝点击