题意
有两种表达方式:一种是RxCy,表示有x行C列。另一种是字母加数字,例如BC23,代表23行,55列(BC为26进制转化,A为1,即BC=26x2+3)
题解
判断输入的两种转态:
- RxCy:
行数x直接输出,列数y进行转换为26进制的字母 - 字母+数字:
先获取前面的字母,然后通过的进制转换为数字,剩下的字符直接输出
知识点
- 解决判断给定的输入是哪种类型
使用sscanf进行格式化输入判断,巧用sscanf的返回值(返回值为成功匹配和赋值的个数)
if(sscanf(a,"%*c%d%*c%d",&r,&c)==2)
- 解决26进制的转换问题
将数字转换为26进制的字母表示,巧用递归回溯输出字母,其中65的值为’A’的ASCII码值
void g(int t){
if(t){
g((t-1)/26);
putchar(65+(t-1)%26);
}
}
代码
#include <iostream>
#include <cstdio>
#include <cctype>
using namespace std;
void g(int t){
if(t) {
g((t-1)/26);
putchar(65+(t-1)%26);
}
}
int main(){
char a[100];
int t,i,r,c,ans;
cin>>t;
getchar();
while(t--) {
gets(a);
ans=0;
if(sscanf(a,"%*c%d%*c%d",&r,&c)==2){
g(c);
cout<<r<<endl;
}
else{
for(i=0;isalpha(a[i]);i++)
ans=26*ans+a[i]-'A'+1;
printf("R%sC%d\n",a+i,ans);
}
}
return 0;
}