c*2
2 楼
哪里违反规定了?
s*a
3 楼
A-Z, AA-AZ - ZZ : total 27*26 possibility, so 每个位上-进位不一样
第一个是26进制,往后都是27进制
第一个是26进制,往后都是27进制
h*n
4 楼
got it,多谢楼上的回答
h*n
5 楼
纠正一下,貌似每个位都是26进制
h*n
6 楼
share一下我的代码,欢迎指正:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
class ExcelIndexConversion
{
static int CharToInt(char c)
{
if (c < 'A' || c > 'Z')
return -1;
return (int)(c - 'A' + 1);
}
static char IntToChar(int digit)
{
if (digit < 1 || digit > 26)
return '#';
return (char)(digit + 'A' -1);
}
static int IndexToNum(String index)
{
int res = 0;
int digit = -1;
for (int i = 0; i < index.Length; i++)
{
digit = CharToInt(index[i]);
if (digit < 0) return -1;
res = res * 26 + digit;
}
return res;
}
static string NumToIndex(int num)
{
StringBuilder tmpStr = new StringBuilder();
StringBuilder resStr = new StringBuilder();
int digit;
if (num < 1)
{
resStr.Append("#");
return resStr.ToString();
}
else if (num < 27)
{
//如果只有一位要进行特别处理。。。
return IntToChar(num).ToString();
};
do
{
digit = num % 26;
tmpStr.Append(IntToChar(digit));
num = num / 26;
}while(num != 0 );
String tmp = tmpStr.ToString();
//reverse the string
for (int i = tmp.Length - 1; i >= 0; i--)
{
resStr.Append(tmp[i]);
}
return resStr.ToString();
}
static void Main(string[] args)
{
String testIndex = "AABB";
String resString;
int res = IndexToNum(testIndex);
resString = NumToIndex(res);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
class ExcelIndexConversion
{
static int CharToInt(char c)
{
if (c < 'A' || c > 'Z')
return -1;
return (int)(c - 'A' + 1);
}
static char IntToChar(int digit)
{
if (digit < 1 || digit > 26)
return '#';
return (char)(digit + 'A' -1);
}
static int IndexToNum(String index)
{
int res = 0;
int digit = -1;
for (int i = 0; i < index.Length; i++)
{
digit = CharToInt(index[i]);
if (digit < 0) return -1;
res = res * 26 + digit;
}
return res;
}
static string NumToIndex(int num)
{
StringBuilder tmpStr = new StringBuilder();
StringBuilder resStr = new StringBuilder();
int digit;
if (num < 1)
{
resStr.Append("#");
return resStr.ToString();
}
else if (num < 27)
{
//如果只有一位要进行特别处理。。。
return IntToChar(num).ToString();
};
do
{
digit = num % 26;
tmpStr.Append(IntToChar(digit));
num = num / 26;
}while(num != 0 );
String tmp = tmpStr.ToString();
//reverse the string
for (int i = tmp.Length - 1; i >= 0; i--)
{
resStr.Append(tmp[i]);
}
return resStr.ToString();
}
static void Main(string[] args)
{
String testIndex = "AABB";
String resString;
int res = IndexToNum(testIndex);
resString = NumToIndex(res);
}
}
}
相关阅读