CF 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.

Sample test(s)
input
2
R23C55
BC23
output
BC23
R23C55

本质是十进制转26进制,难点在于这个26进制是没有零的,因此当当前的n的能被26整除时,置为‘Z’,同时向前借一位,也就是将n减一,因为n减一的效果就是减去26的当前次方。

还有一个更简洁的递归版本,还没有完全理解,理解后补上。

 1 #include <cstdio>
 2 #include <cctype>
 3 using    namespace    std;
 4 
 5 void    fx(int);
 6 int    main(void)
 7 {
 8     int    t,r,c;
 9     char    box[100];
10 
11     scanf("%d",&t);
12     while(t --)
13     {
14         scanf(" %s",box);
15         if(sscanf(box,"%*c%d%*c%d",&r,&c) == 2)
16         {
17             fx(c);
18             printf("%d
",r);
19         }
20         else
21         {
22             int    i;
23             for(c = 0,i = 0;box[i];i ++)
24                 if(isalpha(box[i]))
25                     c = c * 26 + box[i] - 'A' + 1;
26                 else
27                     break;
28             printf("R%sC%d
",&box[i],c);
29         }
30     }
31 
32     return    0;
33 }
34 
35 void    fx(int n)
36 {
37     char    ans[100];
38     int    j = 0;
39 
40     while(n)
41     {
42         if(n % 26 == 0)
43         {
44             ans[j] = 'Z';
45             n -= 1;
46         }
47         else
48             ans[j] = n % 26 - 1 + 'A';
49         n /= 26;
50         j ++;
51     }
52     while(j --)
53         printf("%c",ans[j]);
54 }
原文地址:https://www.cnblogs.com/xz816111/p/4399696.html