codeforces_1B Spreadsheets

题目链接  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


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值