您的位置:首页 > 其它

CodeForces - 1B Spreadsheets

2014-11-05 21:39 316 查看
模拟Excel表格的行和列。。

其实就是十进制和二十六进制的转换,只不过二十六进制没有零元。。

在CF上看见了大神写的代码,赶紧贴上,膜拜一下。代码能力太差了......

#include <cstdio>
void fun(int x)
{
    if(x)
    {
        fun((x-1)/26);
        printf("%c", (x-1)%26+'A');
    }
}
int main()
{
    int n, i;
    scanf("%d%*c", &n);
    while(n--)
    {
        char s[600], *p;
        int x, y;
        gets(s);
        if(sscanf(s, "%*c%d%*c%d", &x, &y)==2)
        {
            fun(y);
            printf("%d\n", x);
        }
        else
        {
            for(x=0, p=s; *p>64; p++)
                x=x*26+*p-64;
            printf("R%sC%d\n", p, x);
        }
    }
    return 0;
}


这是练习赛的时候写的代码。没有整理,显得很乱。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <sstream>
using namespace std;

int n;
string str;

int judge()
{
    int cnt=0, sign=0;
    for(int i=0; i<str.length(); i++)
        if(str[i]>='A' && str[i]<='Z')
        {
            if(sign==0)
                cnt++, sign=1;;
        }
        else sign=0;
    return cnt;
}

int main()
{
    cin>>n;
    while(n--)
    {
        cin>>str;
        int op=judge();
        if(op==1)
        {
            cout<<"R";
            int num=0, i;
            for(i=0; str[i]>='A' && str[i]<='Z'; i++)
                num=num*26+str[i]-'A'+1;
            for(; i<str.length(); i++)
                cout<<str[i];
            cout<<"C"<<num<<endl;
        }
        else
        {
            int sign=0, num=0;
            for(int i=0; i<str.length(); i++)
            {
                if(str[i]=='C')
                    sign=1;
                if(sign && str[i]>='0' && str[i]<='9')
                    num=num*10+str[i]-'0';
            }
            vector<int> v;
            v.clear();
            while(num)
            {
                v.push_back((num-1)%26);
                num=(num-1)/26;
            }
            for(int i=v.size()-1; i>=0; i--)
                printf("%c", v[i]+'A');
            for(int i=1; i<str.length(); i++)
            {
                if(str[i]=='C')
                    break;
                cout<<str[i];
            }
            cout<<endl;
        }
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: