Codeforces Beta Round #1 B. Spreadsheets

1,题目描述:

B,电子表格
一些受欢迎的电子表格(例如Excel)用下列的编号方法编码列数。第一列为A,第二列为B等等。直到26标记为Z。然后是两个字母的编号:第27列是AA,第28列是AB,第52列是AZ,在ZZ后面是三个字母的编号等等。
行数从1开始。每个格子名字包括列数和行数。比如,BC23是由列55和23行而来的。
有时也可以用另一种计数系统,比如RXCY,X,Y是整数代表具体的列数和行数。例如R23C55代表BC23.
你的任务是写一个程序在给你的格子坐标,通过另一种编码系统。
输入:
第一行包括一个整数(1~1e5)坐标的案例数。然后有n行。所有的坐标都是合法的,所有的列数和行数不超过1e6
输出:
写n行,每一行包括一个坐标的另一种编码系统。

2,题目链接:##http://codeforces.com/contest/1/problem/B

3,通过的代码:

  1. 自己的代码:
#include<iostream>
#include<string>
using namespace std;
int main(){
    ios::sync_with_stdio(false);cin.tie(0);
    int T;cin>>T;
    while(T--){
        string str;cin>>str;int len=str.length();int i,j,flag=0;
        for(i=0;i<len;i++)if(str[i]>='0'&&str[i]<='9')break;
        for(j=i;j<len;j++)if(str[j]>='A'&&str[j]<='Z')flag=1;
        if(flag){
            int a=0,b=0;
            for(i=1;;i++){
                if(str[i]=='C')break;
                a=a*10+(str[i]-'0');
            }
            for(j=i+1;j<len;j++)b=b*10+(str[j]-'0');
            string ans;
            while(b){
                if(b%26==0)ans+='Z',b=b/26-1;
                else ans+=b%26+'A'-1,b/=26;

            }
            for(int t=ans.length()-1;t>=0;t--)cout<<ans[t];
            cout<<a<<endl;
        }else{
            int a=0,b=0;
            for(i=0;;i++){
                if(str[i]>='0'&&str[i]<='9')break; 
                a=a*26+(str[i]-'A'+1);  
            }
            for(j=i;j<len;j++)b=b*10+(str[j]-'0');
            cout<<"R"<<b<<"C"<<a<<endl;
        }
    }
}

2.递归做法的代码:

#include<cstdio>
void solve(int n){if(n){solve((n-1)/26);putchar(65+(n-1)%26);}}
int main(){
    int T,x,y;scanf("%d",&T);
    getchar();
    char s[64],*p;
    while(T--){
        gets(s);
        if(sscanf(s,"%*c%d%*c%d",&x,&y)==2){
            solve(y);printf("%d\n",x);
        }else{
            for(x=0,p=s;*p>64;++p)x=x*26+*p-64;
            printf("R%sC%d",p,x);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/light2chasers/article/details/80754782
今日推荐