cf # 1 B. Spreadsheets

本文介绍了如何将C++代码中表示单元格坐标的方式从RC形式转换为字母+数字形式,并详细解释了两种数制之间的转换逻辑与实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 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;
    }
    


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值