CF_1B_Spreadsheets 进制转换


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 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<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const int INF =0x3f3f3f3f;
char s[230];


bool isch(char x)
{
    return 'A'<=x&&x<='Z';
}
bool isnum(char x)
{
    return '0'<=x&&x<='9';
}
int y,x,len;
int gettype()
{
   len=strlen(s);
    int cnt=0;
    for(int i=1;i<len;i++)
    {
        if(isch(s[i-1])&&isnum(s[i])||isch(s[i])&&isnum(s[i-1]))
        {
            cnt++;
        }

    }
    if(cnt==1) return 1;
    return 2;



}
void work1()
{

    int p;
    for(int i=1;i<len;i++)
    {
        if(isch(s[i-1])&&isnum(s[i]))
        {
           p=i-1;
           break;
        }
    }
    y=0;
    for(int i=0;i<=p;i++)
    {
        int t=s[i]-'A'+1;
         y=26*y+t;

    }
    x=0;
    for(int i=p+1;i<len;i++)
    {
        int t=s[i]-'0';
        x=10*x+t;
    }


}

void work2()
{
     int p;
    for(int i=1;i<len;i++)
    {
        if(isch(s[i])&&isnum(s[i-1]))
        {
           p=i-1;
           break;
        }
    }
    x=0;
    for(int i=1;i<=p;i++)
    {
         int t=s[i]-'0';
        x=10*x+t;
    }

    y=0;
      for(int i=p+2;i<len;i++)
    {
         int t=s[i]-'0';
        y=10*y+t;
    }


}

void write2()
{
    string ans="R";

    string tmp="";

    int t=x;
    while(t)
    {
        int k=t%10;
        tmp=char('0'+k)+tmp;
        t/=10;
    }
    ans+=tmp;
    ans+="C";

     t=y;
    tmp="";
    while(t)
    {
         int k=t%10;
        tmp=char('0'+k)+tmp;
        t/=10;
    }
    ans+=tmp;
   cout<<ans<<endl;



}

void write1()
{
    string ans="";

    string tmp="";

    int t=y;
    while(t)
    {

        int k=t%26;

        if(k)
        {
             tmp= char('A'+k-1)+tmp;
             t/=26;
        }

        else//如果余数为0,那么证明最后一位是Z,
        {
             tmp='Z'+tmp;
             t=(t-26)/26;//这个地方要注意,如果最后以为是Z,那么要减去26,再作处理。
             //AZ,这种用字母书写的形式跟我们平时十进制处理不大一样,因为十进制末尾一开始是0,
             //而这种书写方式一开始就是A,表示1。
             //所以总结,除数应该是每一位能表示不同数的个数,比如这里是26,十进制就是10。
             
        }

    }
    ans+=tmp;

    tmp="";
    t=x;
    while(t)
    {
        int k=t%10;
        tmp=char('0'+k)+tmp;
        t/=10;
    }
    ans+=tmp;
    cout<<ans<<endl;


}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s",s);
        int type=gettype();
        if(type==1)  work1();
        else  work2();
       if(type==1)  write2();
       else  write1();
    }

   return 0;
}
/*
BZ26
*/




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值