您的位置:首页 > 其它

codeforces_1B Spreadsheets

2013-04-23 22:37 302 查看
题目链接  http://www.codeforces.com/problemset/problem/1/B

题目大意:两种表示法之间的转换。第一种:字母部分(列)+数字部分(行);第二种:R+行+C+列。

 

//C++代码
#include<iostream>
#include<string>
#include<cctype>
using namespace std;

int main(){
int n;
cin>>n;
while(n--){
string s;
cin>>s;
int len=s.size(),i=0,j,x,y;
while(isalpha(s[i])) i++;
while(isdigit(s[i]) && i<len) i++;
if(i==len){
x=y=j=0;
while(isalpha(s[j])) y=26*y+(s[j++]-'A'+1);
while(j<len) x=10*x+(s[j++]-'0');
cout<<"R"<<x<<"C"<<y<<endl;
}
else{
x=y=0,j=1;
while(isdigit(s[j])) x=10*x+(s[j++]-'0');
j++;
while(j<len) y=10*y+(s[j++]-'0');
char ch[10];
j=0;
while(y>0){
int z=y%26;
if(z==0){
ch[j]='Z';
y-=26;
}
else ch[j]=char(z+64);
y/=26;
j++;
}
for(i=j-1;i>=0;i--) cout<<ch[i];
cout<<x<<endl;
}
}
return 0;
}

 

#python2.7
n=input()
S=map(raw_input,['']*n)

for s in S:
ans=''
if s[0]=='R' and s[1].isdigit() and 'C' in s:
i=s.index('C')
m=int(s[i+1:])
while m:
ans=chr(ord('A')+(m-1)%26)+ans
m=(m-1)/26
ans+=s[1:i]
else:
i=m=0
while s[i].isupper():
m=m*26+(ord(s[i])-ord('A')+1)
i+=1
ans='R'+s[i:]+'C'+str(m)
print ans


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: