【codeforces】1B Spreadsheets
题目链接:Spreadsheets
题目:
Spreadsheets
time limit per test:10 seconds
memory limit per test:64 megabytes
input:standard input
output:standard output
In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second — number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 — AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.
The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23.
Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.
Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.
Input
The first line of the input contains integer number n (1 ≤ n ≤ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106 .
Output
Write n lines, each line should contain a cell coordinates in the other numeration system.
Examples
input
2 R23C55 BC23output
BC23 R23C55
分析:
大体意思是两种行列表示形式的转换。比如第23行55列可表示为R23C55,也可表示为BC23。
整体思路是先通过输入,判断是哪种表示,然后再分别处理输出。我的方式是先输入字符串,再将其行列都转为整型值,然后分别处理输出。
另外注意判断是哪种形式时, 要考虑到两种形式均可能开头有R 且 字符串中含C;也均可能是开头含R和一个数字。所以我是将三者综合判断的。(后来看了别人的做法,发现可以通过sscanf(str,"R%dC%d",&r,&c)的方式获取r和c的值,此外返回值返回成功读到的值的个数;所以可以顺便用此判断为哪种表示形式。)
对形式的处理输出实际上类似于进制转换,由字母到数字,要注意52这种的 转为字母是AZ,刚开始的思路是直接c对26取余 然后-1+‘A’;但这样,当c%26等于0时,结果就不对了;后来发现直接将a-z的范围看做0-25即可,也就是,直接c-1对26取余即可。
另外因为数组开得比较大,刚开始用了memset初始化数组,结果超时了,后来去掉以后就ac了。
代码:
//op=0 R1C2--A2
//op=1 A2--R1C2
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char cell[1000009],s[1000009];
int main()
{
int n,r,c,i,len,op;
scanf("%d",&n);
while (n--)
{
scanf("%s",cell);
len = strlen(cell);
r=0; c=0;
/*判断为哪种表示*/
if (cell[0]=='R' && isdigit(cell[1]) && (strchr(cell,'C')-cell)>0) op=0;
else op=1;
//printf("?=r=%d,STRCHR=%d,%d,op=%d\n",cell[0]=='R',(strchr(cell,'C')-cell),cell[0]=='R'&&(strchr(cell,'C')-cell),op);
/*计算r、c,处理并输出*/
if (op)
{
i=0; r=0; c=0;
for (i=0; i<len; i++)
{
if (isalpha(cell[i]))
c = 26*c + (cell[i]-'A'+1);
else if (isdigit(cell[i]))
{
if (r==0) { printf("R"); r=1;}
printf("%c",cell[i]);
}
}
printf("C%d\n",c);
}
else
{
for (i=1; i<len; i++)
{
if (i<strchr(cell,'C')-cell)
r = 10*r + (cell[i]-'0');
else if (i>strchr(cell,'C')-cell)
c = 10*c + (cell[i]-'0');
}
i=0;
while (c>0)
{
s[i] = (c-1)%26+'A';
if (c%26)
c /= 26;
else
c = c/26-1;
i++;
}
for (i=i-1; i>=0; i--)
printf("%c",s[i]);
printf("%d\n",r);
}
}
return 0;
}
推荐阅读
-
Codeforces Round #595 (Div. 3)D1D2 贪心 STL
-
『ACM C++』 Codeforces | 1066B - Heaters
-
『ACM C++』 Codeforces | 1003C - Intense Heat
-
Codeforces 939A题,B题(水题)
-
CodeForces 29D Ant on the Tree
-
Codeforces Global Round 9-C. Element Extermination
-
codeforces 712B Memory and Trident
-
CodeForces 962D Merge Equals
-
Codeforces Round #655 (Div. 2) A. Omkar and Completion
-
CodeForces 1326E - Bombs