Codeforces Beta Round #1 B. Spreadsheets

来源:互联网 发布:手电筒软件哪个好 编辑:程序博客网 时间:2024/05/22 15:24

题意:两种表示行列的格式之间的相互转换。第一种格式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 1000005int 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
原创粉丝点击