CodeForces 1B Spreadsheet

字符串处理+计算吧

用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);
        }
    }
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值