Last active
February 21, 2023 08:20
-
-
Save unilecs/9816810892bda78415466dcb8b154a97 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
public class Program | |
{ | |
public static string ConvertToExcelTitle(int colNum) | |
{ | |
string result = ""; | |
while (colNum > 0) | |
{ | |
// нумерация символов начинается с 0, в Excel c 1 | |
colNum--; | |
// получаем код текущего символа | |
int lastSymbolCode = colNum % 26; | |
// преобразуем код в символ и добавляем в результат | |
result = (char)(lastSymbolCode + 65) + result; | |
// "двигаемся" к следующему символу | |
colNum /= 26; | |
} | |
return result; | |
} | |
public static void Main() | |
{ | |
Console.WriteLine("UniLecs"); | |
// tests | |
Console.WriteLine(ConvertToExcelTitle(1)); // A | |
Console.WriteLine(ConvertToExcelTitle(28)); // AB | |
Console.WriteLine(ConvertToExcelTitle(701)); // ZY | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment