Codeforces 1 B. Spreadsheets

题意:

EXCEL 单元格位置表示方法相互转化  R23C55 <=> RC23

思路

AAA <=> 26 ^ 2 + 26 + 1

用到知识点:

正则表达式匹配

字符串反转

字符串出现位置

C# 10 .net6 代码

using System.Text;
using System.Text.RegularExpressions;

int n = int.Parse(Console.ReadLine()!);
while (n-- > 0)
{
    string str = Console.ReadLine()!;
    Regex regex = new Regex(@"^R\d+C\d+$");
    if (regex.IsMatch(str))
    {
        int indexC = str.IndexOf('C');
        int r = int.Parse(str.Substring(1, indexC - 1));
        int c = int.Parse(str.Substring(indexC + 1, str.Length - indexC - 1));
        StringBuilder sb = new StringBuilder();
        while (c > 0)
        {
            sb.Append((char)('A' + (c - 1) % 26));
            c -= (c - 1) % 26 + 1;
            c /= 26;
        }
        Console.WriteLine($"{new string(sb.ToString().ToCharArray().Reverse().ToArray())}{r}");
    }
    else
    {
        int indexNum = str.IndexOfAny("0123456789".ToCharArray());
        string cStr = str.Substring(0, indexNum);
        int c = 0;
        foreach (var item in cStr)
        {
            c *= 26;
            c += item - 'A' + 1;
        }
        Console.WriteLine($"R{str.Substring(indexNum, str.Length - indexNum)}C{c}");
    }
}

 

posted on 2022-12-05 18:23  luobo67  阅读(27)  评论(0)    收藏  举报

导航