1. 程式人生 > 其它 >Codeforces 1 B. Spreadsheets

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}"); } }