题目:
描述:用两种方式表示行和列:RC法,R行数C列数;excel法:前面用A——Z表示列数,满Z进A;后面用数字表示行数。
题解:就是26进制的一个转换,但是在十进制转二十六进制的时候,要注意特判取模为0的情况,此时应输出z。
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <cmath>
using namespace std;
char str[100];
void EtoR(char str[])
{
int row = 0, collum = 0;
for(int i = 0;i < strlen(str);i++)
{
if(isalpha(str[i]))
{
collum *= 26;
collum += str[i] - 'A' + 1;
}
else
{
row *= 10;
row += str[i]-'0';
}
}
printf("R%dC%d\n",row,collum);
}
void RtoE(char str[])
{
int row = 0,collum = 0;
char temp[100];
int i = 1;
for(;str[i] != 'C' && i < strlen(str);i++)
{
row *= 10;
row += str[i] - '0';
}
i++;
for(;i < strlen(str) ;i++)
{
collum *= 10;
collum += str[i] - '0';
}
i = 0;
while(collum != 0)
{
int tmp = collum % 26;
if(tmp == 0)
tmp = 26;
temp[i++] = tmp + 64;
collum /= 26;
if(tmp == 26)collum--;
}
while(i--)printf("%c",temp[i]);
printf("%d\n",row);
}
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
scanf("%s",str);
int flag = 0;
int i = 0;
while(isalpha(str[i]) && i < strlen(str))i++;
for(;i < strlen(str);i++)
{
if(str[i] == 'C')
{
flag = 1;
break;
}
}
if(flag)
RtoE(str);
else
EtoR(str);
}
return 0;
}