cf # 1 B. Spreadsheets

  • http://codeforces.com/contest/1/problem/B
  1. B. 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
    

    对一个网格有两种表示形式,一个是R***C***形式,另一个是    字母 +数字形式,两个转换关系可以看样例;;
  2. #include <iostream>
    #include <stdio.h>
    #include <string.h>
    using namespace std;
    int myabs(int a,int b)
    {
        if(a>b)
            return a-b;
        else
            return b-a;
    }
    int f(char s[])
    {
        int ans=0;
        for(int i=0; i<strlen(s); i++)
        {
            ans=(s[i]-'0')+ans*10;
        }
        return ans;
    }
    int altonum(char s[])
    {
        int ans=0;
        for(int i=0; i<strlen(s); i++)
        {
            ans=ans*26+(int)(s[i]-'A'+1);
        }
    
        return ans;
    
    }
    int main()
    {
        char ans1[50],ans2[50];
        char str[100];
        int ci,ri;
        int n,ans;
        int k,g;
        int num[100];
        cin>>n;
        while(n--)
        {
            cin>>str;
            int len=strlen(str);
            ci=-1;
            ri=-1;
            for(int i=0; i<len; i++)
            {
                if(str[i]=='R')
                    ri=i;
                if(str[i]=='C')
                    ci=i;
            }
            if(myabs(ci,ri)>1&&str[ci-1]-'0'>=0&&str[ci-1]-'0'<=9&&ci!=-1&&ri!=-1)///R C 之间必须是数字,
            {
                //cout<<1<<endl;
                k=0,g=0;
                int j;
                for(int i=0; i<len; i++)
                {
                    if(str[i]=='R')
                    {
                        for(j=i+1; j<len; j++)
                        {
                            if(str[j]=='C')
                            {
                                //  cout<<"j"<<j<<endl;
                                ans2[k]='\0';
                                break;
                            }
                            ans2[k++]=str[j];
                        }
                        for(j=j+1; j<len; j++)
                        {
                            ans1[g++]=str[j];
                        }
                        ans1[g]='\0';
                        break;
                    }
    
                }
                ans=f(ans1);
                k=0;
                while(ans)
                {
                    num[k++]=ans%26;
                    int t=ans;
                    ans=ans/26;
                    if(t%26==0)
                ans--;
                }
                for(int i=k-1; i>=0; i--)
                {
                    if(num[i]==0)
                        cout<<"Z";
                    else
                    cout<<char(num[i]-1+'A');
                }
                cout<<ans2<<endl;
            }
            else
            {
                k=0,g=0;
                for(int i=0; i<len; i++)
                {
                    if(isalpha(str[i]))
                        ans2[k++]=str[i];
                    else if(str[i]-'0'<=9&&str[i]-'0'>=0)
                        ans1[g++]=str[i];
                }
                ans1[g]='\0';
                ans2[k]='\0';
                cout<<"R"<<ans1<<"C";
                ans=altonum(ans2);
                cout<<ans<<endl;
            }
    
        }
        return 0;
    }
    


你可能感兴趣的:(cf # 1 B. Spreadsheets)