字符串处理+计算吧
用python,使用正则表达式来读取数据更方便一些。
import re
def convert(d):
s=''
while (d > 0):
t=d%26
d/=26
if (t == 0):
s+=('Z')
d-=1
else:
s+=chr(ord('A')+t-1)
s=s[::-1]
return s
def reconv(ts):
res=0
ls=len(ts)
cnt=ls-1;
for i in ts:
res+=(ord(i)-ord('A')+1)*pow(26,cnt)
cnt-=1
return res
prob=int(raw_input())
while prob!=0:
str=raw_input()
tmp=re.findall("R(\d+)C(\d+)",str)
if tmp:
t1=int(tmp[0][0])
t2=int(tmp[0][1])
ans=convert(t2)
ans+=tmp[0][0]
print ans
else:
tmp=re.findall("([A-Z]+)(\d+)",str)
print 'R%sC%d' % (tmp[0][1],reconv(tmp[0][0]))
prob-=1
附cpp版
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
char ttstr[100000];
int pow(int s,int p)
{
int r,i;
r=1;
for (i=0; i<p; i++)
{
r*=s;
}
return r;
}
int convert(int ls)
{
int res,i,t;
res=0;
for (i=ls-1; i>=0; i--)
{
res+=(ttstr[i]-'A'+1)*pow(26,ls-i-1);
}
return res;
}
char* reconv(int d)
{
char str[100000],ts[100000];
int ls,t;
ls=0;
while (d > 0)
{
t=d%26;
d/=26;
if (t == 0)
{
str[ls]='Z';
d--;
}
else
{
str[ls]=t+'A'-1;
}
ls++;
}
for (t=0; t<ls; t++)
{
printf("%c",str[ls-t-1]);
}
return ts;
}
int main()
{
int prob,i,ls,j,t1,t2;
scanf("%d",&prob);
while (prob--)
{
scanf("%s",ttstr);
t1=-1;
if (ttstr[0] == 'R')
{
if (!(ttstr[1] <= '9' && ttstr[1] >= '0'))
{
t1=-1;
}
else
{
for (i=2; ttstr[i]; i++)
{
if (ttstr[i] == 'C')
{
t1=0;
break;
}
}
}
}
if (t1 == -1)
{
i=0;
while (ttstr[i] <= 'Z' && ttstr[i] >= 'A') i++;
t1=convert(i);
sscanf(ttstr+i,"%d",&t2);
printf("R%dC%d\n",t2,t1);
}
else
{
sscanf(ttstr,"R%dC%d",&t1,&t2);
reconv(t2);
printf("%d\n",t1);
}
}
}