codeforces-1B. Spreadsheets

1B. Spreadsheets

                time limit per test10 seconds    memory limit per test64 megabytes

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.

input
2
R23C55
BC23
output
BC23
R23C55

题目大意:两种形式的表示转化。第一种R(num1)C(num2),第二种把num2转化成字母表示:A表示1,AA就是27,AZ是52。

题目思路:模拟。

题目链接:1B Spreadsheets

以下是代码:

#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
using namespace std;
string t = "ZABCDEFGHIJKLMNOPQRSTUVWXYZ";
int main(){
    int n;
    cin >> n;
    while(n--)
    {
        char s[100];
        cin >> s;
        int x,y;
        if(sscanf(s,"%*c%d%*c%d",&x,&y)==2)      //判断初始形式
        {
             string ans =  "";                  //用于记录y转化的字母形式
             while(y / 26 >= 1)
             {
                 ans += t[y % 26];      
                 if (y % 26 == 0)               //注意:如果能够整除,后一位需减一。
                 {
                    y /= 26;
                    y--;
                 }
                 else
                    y /= 26;
             }
             if (y != 0)
                 ans += t[y % 26];       
             reverse(ans.begin(),ans.end());          
             cout << ans;
             printf("%d\n",x);
        }
        else
        {
            int cnt = 0,sum = 0;
            double ans = 0;
            for( int i = 0; i < strlen(s); i++ )         //用cnt记录字母段的长度
            {
                if ( isalpha(s[i]) )
                {
                    cnt++;
                }
                else            
                {
                    break;
                }
            }
            for (int  i = cnt - 1,j = 0; i >= 0; i--,j++)       
            {
                if (j == 0)
                    sum += s[i] - 'A' + 1;
                else
                    sum += (s[i] - 'A' + 1) * (int)pow(26.0,j);     //相当于26进制
            }
            printf("R");
            for (int i = cnt; i < strlen(s); i++)
            {
                cout << s[i];
            }
            printf("C%d\n",sum);
        }
    }

    return 0;
}

你可能感兴趣的:(模拟)