欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

CodeForces - 1B Spreadsheets【模拟】

程序员文章站 2022-05-09 16:18:27
...

【题目描述】
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.

【输入】
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 .

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

【样例输入】
2
R23C55
BC23

【样例输出】
BC23
R23C55

题目链接:http://codeforces.com/problemset/problem/1/B

代码如下:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        string s;
        cin>>s;
        int pos=0;
        while(isalpha(s[pos])) pos++;
        while(isdigit(s[pos]) && pos<s.size()) pos++;
        if(pos==s.size())
        {
            int p=0,x=0,y=0;
            while(isalpha(s[p])) y=26*y+s[p++]-'A'+1;
            while(p<s.size()) x=10*x+s[p++]-'0';
            cout<<"R"<<x<<"C"<<y<<endl;
        }
        else
        {
            int p=1,x=0,y=0;
            while(isdigit(s[p])) x=x*10+s[p++]-'0';
            p++;
            while(p<s.size()) y=y*10+s[p++]-'0';
            char ch[10];
            int t=0;
            while(y)
            {
                if(y%26==0)
                {
                    ch[t++]='Z';
                    y-=26;
                }
                else ch[t++]=char(y%26+64);
                y/=26;
            }
            for(int j=t-1;j>=0;j--)
                cout<<ch[j];
            cout<<x<<endl;
        }
    }
    return 0;
}