Codeforces Beta Round #1 B. Spreadsheets

题意:两种表示行列的格式之间的相互转换。第一种格式AA123  表示AA表示第27列123表示123行 以此类推  第二种格式:R123C23表示第23列123行

思路:首先分辨出是哪种类型的字符串,然后根据字母表示相当于26进制的思路进行转换。

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <queue>
#include <vector>
#include <map>
using namespace std;
typedef long long LL;
typedef unsigned long long UL;
#define MS(x,y) memset(x,y,sizeof(x))
#define rpt(i,l,r) for(int i=l;i<=r;i++)
#define rpd(i,r,l) for(int i=r;i>=l;i--)
LL gcd(LL a,LL b){ return b==0?a:gcd(b,a%b);}
#define N 1000005
int main(){
    int n;
    scanf("%d",&n);
    while(n--){
        char str[N];
        scanf("%s",str);
        int kind=0;
        int len=strlen(str);
        for(int i=0;i<len;i++){
            if(kind==0&&str[i]<='9'&&str[i]>='0') kind=1;
            if(kind==1&&str[i]=='C') kind=2;
        }
        if(kind==1){
            int k=1;
            int r=0;
            printf("R");
            int i=0;
            char temp[100];
            int pos=0;
            while(str[i]>='A'&&str[i]<='Z'){
                temp[pos]=str[i];
                pos++;
                i++;
            }
            for(int j=pos-1;j>=0;j-- ){
                r=r+(temp[j]-'A'+1)*k;
                k=k*26;
            }
             while(str[i]!='\0'){
                printf("%c",str[i]);
                i++;
            }
            printf("C%d\n",r);
        }
        if(kind==2){
            int r=0;
            int c=0;
            int i=1;
            while(str[i]!='C'){
                r=r*10+str[i]-'0';
                i++;
            }
            i++;
            while(str[i]!='\0'){
                c=c*10+str[i]-'0';
                i++;
            }
            char temp[1000];
            int pos=0;
            while(c!=0){
                if(c%26==0) temp[pos]='Z';
                else
                    temp[pos]=c%26-1+'A';
                c=(c-1)/26;//这里注意要减1  比如Z=26  如果直接除以26值为1,但应该为0了
                pos++;
            }
            for(int i=pos-1;i>=0;i--){
                printf("%c",temp[i]);
            }
            printf("%d\n",r);
        }
    }
    return 0;
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值