1B. Spreadsheets

1B. Spreadsheets

time limit :per test10 seconds
memory limit :per test64 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 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
2
R23C55
BC23
output
BC23
R23C55

注意字符串与数字转化处

#include <iostream>
#include <string>
using namespace std;

int judge(string str){
    int last_pos=0;
    for(int i=0;i<str.length();i++){
        if(str[i]>='A'&&str[i]<='Z')
            last_pos = i;
    }
    bool isnumber = false; 
    for(int i=0;i<=last_pos ;i++){
        if(str[i]<'A'||str[i]>'Z'){
                isnumber = true;
                break;
        }
    }
    return isnumber?2:1;
}

string firstTosecond(string str){
    // BC23->RXCY
    int col = 0,row = 0;
    int i;
    string res = "R";
    for(i=0;str[i]>='A'&&str[i]<='Z';i++){
        col = col*26+str[i]-'A'+1;
    }
    res+=str.substr(i);
    res+="C";
    string temp_col;
    string begin = "0";
    while(col){
        string begin_temp = begin;
        int offset = col%10;
        begin_temp[0]=begin[0]+offset;
        temp_col=begin_temp+temp_col;
        col/=10;
    }
    res+=temp_col;
    return res;
}

string secondTofirst(string str){
    //RXCY->BC23
    string res;
    string row;
    int i;
    for(i=1;str[i]!='C';i++){
        row+=str[i];
    }
    int col=0;
    for(i++;i<str.length();i++){
        col= col*10+str[i]-'0';
    }
    string temp_col;
    string begin = "A";
    while(col){     // Attention: col = 26|| 27
        string begin_temp = begin;
        int offset = col%26;
        if(offset==0)
            offset=26;
        begin_temp[0]=begin[0]-1+offset;
        temp_col=begin_temp+temp_col;
        col=(col-1)/26;  
    }
    res = temp_col+row;
    return res;
}


int main(int argc, char *argv[])
{
    int n;
    cin>>n;
    while(n--){
        string str;
        cin>>str;
        if(judge(str)==1)
            cout<<firstTosecond(str)<<endl;
        else
            cout<<secondTofirst(str)<<endl;
    }
    return 0;
}
posted @ 2016-06-27 11:53  U_F_O  阅读(219)  评论(0编辑  收藏  举报