• Spreadsheets codeforces1B(模拟+进制转换)


    http://codeforces.com/problemset/problem/1/B

    题意:转换两种行和列的表示方法。

    分析:(弱弱的说下一开始自己并不会写,只是想到AA=26*1+1,就想着用除法和余数来解决了,后来发现这么模拟下去,数据好大啊,心好累。。然后就没有然后了)。后来问的别人,告诉我用26进制转换(我去,我为啥想不到,啥都别说了,看代码)

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <map>
    #include <queue>
    #include <stack>
    #include <math.h>
    
    using namespace std;
    
    #define INF 0x3f3f3f3f
    const int maxn = 1000005;
    typedef long long LL;
    char str[maxn];
    int flag;
    
    void change(int n)///26进制转换
    {
        if(n>26) change((n-1)/26);
    
        printf("%c",(n-1)%26+'A');
    }
    
    
    void solve1()
    {
        int row=0;
        int col=0;
        for(int i=1; i<flag; i++)
        {
            if(isdigit(str[i]))///计算行
                row=row*10+str[i]-'0';
        }
    
        for(int i=flag+1; str[i]; i++)
        {
            if(isdigit(str[i]))///计算列
              col=col*10+str[i]-'0';
        }
    
        change(col);///转换列
    
        printf("%d
    ", row);
    }
    
    void solve2()
    {
        int row = 0;
        int col = 0;
    
        for(int i=0; str[i]; i++)
        {
            if(isupper(str[i]))
                col=col*26+str[i]-'A'+1;///计算列
            else
                row=row*10+str[i]-'0';///计算行
        }
    
        printf("R%dC%d
    ", row, col);
    }
    
    int main()
    {
        int T;
    
        scanf("%d", &T);
    
        while(T --)
        {
            scanf("%s", str);
            flag = 0;
    
            if((str[0]=='R') && isdigit(str[1]))
            {
                for(int i=2; str[i]; i++)
                {
                    if(str[i]=='C')
                    {
                        flag=i;
                        break;
                    }
                }
            }
    
            if(flag) solve1();///判断‘R23C55’这一种情况
            else solve2();///判断‘BC23’这一种情况
    
        }
        return 0;
    }
    View Code
  • 相关阅读:
    牛客网-求和
    牛客网-删除公共字符
    牛客网-字符串中找出连续最长的数字串(好未来)
    牛客网-求数列的和(挖财)
    牛客网-树的高度(小米)
    牛客网-藏宝图(网易)
    【Python学习笔记】
    php平滑升级
    Nginx平滑升级
    linux下线刷硬盘
  • 原文地址:https://www.cnblogs.com/daydayupacm/p/5777682.html
Copyright © 2020-2023  润新知