[英]Figuring out the number corresponding to a letter of the alphabet?
I'm trying to duplicate the way excel provides labels its columns where 我试图复制excel提供标签的方式
A = 1
A = 1
B = 2B = 2
so on and so forth, so that it eventually reaches 等等,以便它最终达到
AB
AB
ACAC
AD广告
etc, etc. 等等
How do I algorithmically take a number (like 52) and convert it to its equivalent alphabet representation? 我如何在算法上取一个数字(如52)并将其转换为等效的字母表示?
string get(int a) {
a--; // Reduce by one to make the values with 1 letter 0..25,
// with two - 26.. 26^2-1 and so on
int val = 0; // number of columns with no more then given number of letters
int number = 0;
while (val < a) {
val = val*26 + 26;
number++;
}
val = (val - 26)/26;
a -= val; // subtract the number of columns with less letters
string res;
for (int i = 0; i < number; ++i) {
res.push_back(a%26 + 'A');
a /= 26;
}
reverse(res.begin(), res.end());
return res;
}
Hope that helps. 希望有所帮助。
for two letters 两封信
#include <iostream>
#include <string>
using namespace std;
string int2alphas(int i) {
string res;
if (i > 26 - 1)
res.push_back(i / 26 + 'A' - 1);
else
res.push_back(' ');
res.push_back(i % 26 + 'A');
return res;
}
void test(int t) {
cout << t << "-" << int2alphas(t) << endl;;
}
int main() {
for (int i = 0; i < 55; i++)
test(i);
}
Convert.ToInt32("52", 26).... And now just create the correct base implementation. Convert.ToInt32(“52”,26)....现在只需创建正确的基本实现。 ?
? homework ?
家庭作业 ?
You may think of writing some algo like: 您可能会想到写一些算法:
ConvertToAlphaCode(number input)
{
Array Chars=[A-Z]
if (number<= 26)
return Chars[number-1]
else
...
}
Have a look: 看一看:
A, B, C, D, ..., Y, Z, AA, AB, AC, AD, ..., AY, AZ, BA, BB, ...
is exactly like: 就像:
0, 1, 2, 3, 4, ..., 9, 10, 11, 12, 13, ..., 19, 20, 21, ...
but with digits A..Z
instead of 0..9
. 但是数字
A..Z
而不是0..9
。 So: 所以:
Algorithmic-ally I'm not sure how I can get a number, like say 52, and convert it to the equivalent alphabet representation..
算法 - 我不知道如何得到一个数字,比如52,并将其转换为等效的字母表示。
You need to use a generic algorithm to convert a number in base-N to base-M (like decimal to hexadecimal), but with N equal to 10 and M equal to 26 (letters), and make sure that you use correct characters to represent the final "digits". 您需要使用通用算法将base-N中的数字转换为base-M(如十进制到十六进制),但N等于10且M等于26(字母),并确保使用正确的字符代表最后的“数字”。 As simple as that!
就如此容易!
This will do it quite well: 这样做会很好:
string calcString(int number)
{
string returnValue = "";
do
{
int rm = number % 26;
returnValue += (char)(rm+ 'A');
number = (number - rm) / 26;
}
while (number > 0);
return returnValue;
}
For example, calcString(11);
例如,
calcString(11);
results in L
. 结果是
L
If this is not precisely the calculation for which you were looking, leave a comment to clarify what you want and I'll come back to change it. 如果这不是你正在寻找的计算,请留下评论以澄清你想要的东西,我会回来改变它。
This will work with all types of letters (small, big). 这适用于所有类型的字母(小,大)。
using namespace std;
int lettervalue (string l) {
l=l[0];
string alpha="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int idx = alpha.find(l)+1;
if(idx>26){
idx=idx-26;
}
return idx;
}
To use it: 要使用它:
cout << lattervalue("e"); //will return 5(int)
From number to letter: 从数字到字母:
static std::string const letters( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
assert( n >= 0 && n < letters.size() );
return letters[n];
From letter to number: 从字母到数字:
static std::string const letters( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
char const* result = std::find(
letters.begin(),
letters.end(),
isupper( static_cast< unsigned char >( l ) );
assert( result != letters.end() );
return result - letters.begin();
EDIT: 编辑:
This just handles a single character in each direction. 这只是处理每个方向上的单个字符。 For more, it's just a base 26 conversion, using the usual conversion routine.
更多的是,它只是一个基础26转换,使用通常的转换例程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.