Codeforces 1B (字符串的26进制处理)

本文解析了CodeForces平台上B题的算法挑战,详细介绍了如何实现10进制与26进制间的坐标转换,包括Excel表格中特定的列标记系统。提供了完整的AC代码示例,帮助读者理解并解决该算法问题。

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 test(s)
Input
2
R23C55
BC23
Output
BC23
R23C55

题目意思:
简单来说就是10进制和26进制的相互转换,给出RXCY转换长YX,其中Y为26进制,模拟题目,不难但是各种陷阱
各种多,看自己的人品了,我都不知道wa了多少次。。。。,还得注意判断是那种转换,是RXCY-->XY?还是
XY—>RXCY

AC代码:
/**
  *@xiaoran
  *题意意思,26进制的转换
  */
#include<iostream>
#include<cstdio>
#include<map>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<cstdlib>
#include<cctype>
#include<cmath>
#define LL long long
using namespace std;
void IntToSta(char *s){
    int sr,sc,len;
    sr=sc=0;
    len=strlen(s);
    for(int i=0;i<len;i++){
        if(isalpha(s[i])){//是字母
            sc=sc*26+(s[i]-'A'+1);
        }
        if(isdigit(s[i])){//是数字
            sr=sr*10+(s[i]-'0');
        }
    }
    printf("R%dC%d\n",sr,sc);
}
void StaToInt(char *s){
    int sr,sc,len;
    //sr=sc=0;
    len=strlen(s);
    sscanf(s,"R%dC%d",&sr,&sc);
    //printf("%d %d\n",sr,sc);

    int res=sc,mes,k=0;
    //res=sc/26; mes=sc%26;
    char ss[58];
    while(res){
        if(res%26==0){
            ss[k++]='A'+res%26-1+26;
            res/=26;
            res-=1;
        }
        else{
            ss[k++]='A'+res%26-1;
        //printf("%c",'A'+res%26);
            res/=26;
        }
    }
    //ss[k]='A'+res-1;
    for(int i=k-1;i>=0;i--) printf("%c",ss[i]);

    printf("%d\n",sr);
    //printf("R%dC%d\n",sr,sc);
}

int main()
{
    int n;
    char s[100];
    scanf("%d",&n);
    while(n--){
        scanf("%s",s);
        int ok1=0,ok2=0;
        ok1=s[0]=='R'&&isdigit(s[1]);
        for(int i=0;i<strlen(s)-1;i++){
            if(s[i]=='C'&&isdigit(s[i+1])) ok2=1;
        }
        //ok1&ok2判断选择哪个函数
        if(ok1&&ok2) StaToInt(s);
        else IntToSta(s);

    }
	return 0;
}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值