从给定的列号中查找Excel列名

MS Excel列有a、B、C、…、Z、AA、AB、AC、…。,AZ,BA,BB,…ZZ,AAA,AAB…。。换言之,第1栏被命名为“A”,第2栏被命名为“B”,第27栏被命名为“AA”。 给定一个列号,找到对应的Excel列名。下面是更多的例子。

null
Input          Output 26             Z 51             AY 52             AZ 80             CB 676            YZ 702            ZZ 705            AAC

幸亏 德姆布拉先生 感谢您在评论中提出以下解决方案。 假设我们有一个数字n,比如说28。所以对应于它,我们需要打印列名。我们需要把剩下的和26个一起拿走。

如果带26的余数为0(意味着26,52,等等),那么我们在输出字符串中放入’Z’,新的n变成n/26-1,因为这里我们认为26是’Z’,而实际上它是相对于’A’的第25个。

同样,如果余数为非零。(比如1、2、3等等)然后我们需要在字符串中相应地插入char,然后n=n/26。

最后,我们反转字符串并打印。

例子: n=700 剩下的(n%26)是24。所以我们在输出字符串中加上X,n变成n/26,也就是26。 余数(26%26)为0。所以我们在输出字符串中加入Z,n变成n/26-1,也就是0。

以下是上述方法的实现。

C++

// C++ program to find Excel
// column name from a given
// column number
#include <bits/stdc++.h>
#define MAX 50
using namespace std;
// Function to print Excel column name for a given column number
void printString( int n)
{
char str[MAX]; // To store result (Excel column name)
int i = 0; // To store current index in str which is result
while (n > 0) {
// Find remainder
int rem = n % 26;
// If remainder is 0, then a 'Z' must be there in output
if (rem == 0) {
str[i++] = 'Z' ;
n = (n / 26) - 1;
}
else // If remainder is non-zero
{
str[i++] = (rem - 1) + 'A' ;
n = n / 26;
}
}
str[i] = ' ' ;
// Reverse the string and print result
reverse(str, str + strlen (str));
cout << str << endl;
return ;
}
// Driver program to test above function
int main()
{
printString(26);
printString(51);
printString(52);
printString(80);
printString(676);
printString(702);
printString(705);
return 0;
}


JAVA

// Java program to find Excel
// column name from a given
// column number
public class ExcelColumnTitle {
// Function to print Excel column
// name for a given column number
private static void printString( int columnNumber)
{
// To store result (Excel column name)
StringBuilder columnName = new StringBuilder();
while (columnNumber > 0 ) {
// Find remainder
int rem = columnNumber % 26 ;
// If remainder is 0, then a
// 'Z' must be there in output
if (rem == 0 ) {
columnName.append( "Z" );
columnNumber = (columnNumber / 26 ) - 1 ;
}
else // If remainder is non-zero
{
columnName.append(( char )((rem - 1 ) + 'A' ));
columnNumber = columnNumber / 26 ;
}
}
// Reverse the string and print result
System.out.println(columnName.reverse());
}
// Driver program to test above function
public static void main(String[] args)
{
printString( 26 );
printString( 51 );
printString( 52 );
printString( 80 );
printString( 676 );
printString( 702 );
printString( 705 );
}
}
// This code is contributed by Harikrishnan Rajan


python

# Python program to find Excel column name from a
# given column number
MAX = 50
# Function to print Excel column name
# for a given column number
def printString(n):
# To store result (Excel column name)
string = [ " " ] * MAX
# To store current index in str which is result
i = 0
while n > 0 :
# Find remainder
rem = n % 26
# if remainder is 0, then a
# 'Z' must be there in output
if rem = = 0 :
string[i] = 'Z'
i + = 1
n = (n / 26 ) - 1
else :
string[i] = chr ((rem - 1 ) + ord ( 'A' ))
i + = 1
n = n / 26
string[i] = ' '
# Reverse the string and print result
string = string[:: - 1 ]
print "".join(string)
# Driver program to test the above Function
printString( 26 )
printString( 51 )
printString( 52 )
printString( 80 )
printString( 676 )
printString( 702 )
printString( 705 )
# This code is contributed by BHAVYA JAIN


C#

// C# program to find Excel
// column name from a given
// column number
using System;
class GFG{
static String reverse(String input)
{
char [] reversedString = input.ToCharArray();
Array.Reverse(reversedString);
return new String(reversedString);
}
// Function to print Excel column
// name for a given column number
private static void printString( int columnNumber)
{
// To store result (Excel column name)
String columnName = "" ;
while (columnNumber > 0)
{
// Find remainder
int rem = columnNumber % 26;
// If remainder is 0, then a
// 'Z' must be there in output
if (rem == 0)
{
columnName += "Z" ;
columnNumber = (columnNumber / 26) - 1;
}
// If remainder is non-zero
else
{
columnName += ( char )((rem - 1) + 'A' );
columnNumber = columnNumber / 26;
}
}
// Reverse the string
columnName = reverse(columnName);
// Print result
Console.WriteLine(columnName.ToString());
}
// Driver code
public static void Main(String[] args)
{
printString(26);
printString(51);
printString(52);
printString(80);
printString(676);
printString(702);
printString(705);
}
}
// This code is contributed by amal kumar choubey


Javascript

<script>
// Javascript program to find Excel
// column name from a given
// column number
// Function to print Excel column
// name for a given column number
function printString(columnNumber)
{
// To store result (Excel column name)
let columnName = [];
while (columnNumber > 0) {
// Find remainder
let rem = columnNumber % 26;
// If remainder is 0, then a
// 'Z' must be there in output
if (rem == 0) {
columnName.push( "Z" );
columnNumber = Math.floor(columnNumber / 26) - 1;
}
else // If remainder is non-zero
{
columnName.push(String.fromCharCode((rem - 1) + 'A' .charCodeAt(0)));
columnNumber = Math.floor(columnNumber / 26);
}
}
// Reverse the string and print result
document.write(columnName.reverse().join( "" )+ "<br>" );
}
// Driver program to test above function
printString(26);
printString(51);
printString(52);
printString(80);
printString(676);
printString(702);
printString(705);
// This code is contributed by rag2127
</script>


输出

ZAYAZCBYZZZAAC

方法2 这个问题类似于将一个十进制数转换为二进制表示,但不是一个只有0和1两个数字的二进制基数系统,这里我们有26个a-Z字符。 所以,我们处理的是基数26,而不是基数二进制。 这不是乐趣的终点,我们在这个数字系统中没有零,因为A代表1,B代表2,所以Z代表26。 为了使问题易于理解,我们分两步解决问题:

  1. 考虑到系统中也有0,请将数字转换为以26为基数的表示形式。
  2. 将表示形式更改为系统中不包含0的表示形式。

怎样下面是一个例子

第一步: 考虑到我们有676号,如何在基座26系统中得到它的表示?就像我们对二进制系统所做的那样,我们不是用2除法和余数,而是用26除法和余数。

Base 26 representation of 676 is : 100 

步骤二 但是,嘿,我们的代表不能是零。正当因为它不是我们数字系统的一部分。我们如何摆脱零?这很简单,但在做这件事之前,让我们提醒一下一个简单的数学技巧:

Subtraction: 5000 - 9, How do you subtract 9 from 0 ? You borrowfrom next significant bit, right.  
  • 在处理零的十进制系统中,我们借用10并从下一个有效值中减去1。
  • 在基数为26的数字系统中,为了处理零,我们借用26并从下一个有效位中减去1。

所以转换100 26 对于没有“0”的数字系统,我们得到(25 26) 26 相同的符号表示为:YZ

以下是相同的实现:

C++

#include <iostream>
using namespace std;
void printString( int n)
{
int arr[10000];
int i = 0;
// Step 1: Converting to number assuming
// 0 in number system
while (n) {
arr[i] = n % 26;
n = n / 26;
i++;
}
// Step 2: Getting rid of 0, as 0 is
// not part of number system
for ( int j = 0; j < i - 1; j++) {
if (arr[j] <= 0) {
arr[j] += 26;
arr[j + 1] = arr[j + 1] - 1;
}
}
for ( int j = i; j >= 0; j--) {
if (arr[j] > 0)
cout << char ( 'A' + arr[j] - 1);
}
cout << endl;
}
// Driver program to test above function
int main()
{
printString(26);
printString(51);
printString(52);
printString(80);
printString(676);
printString(702);
printString(705);
return 0;
}
// This code is contributed by Ankur Goel


JAVA

import java.util.*;
class GFG{
static void printString( int n)
{
int []arr = new int [ 10000 ];
int i = 0 ;
// Step 1: Converting to number
// assuming 0 in number system
while (n > 0 )
{
arr[i] = n % 26 ;
n = n / 26 ;
i++;
}
// Step 2: Getting rid of 0, as 0 is
// not part of number system
for ( int j = 0 ; j < i - 1 ; j++)
{
if (arr[j] <= 0 )
{
arr[j] += 26 ;
arr[j + 1 ] = arr[j + 1 ] - 1 ;
}
}
for ( int j = i; j >= 0 ; j--)
{
if (arr[j] > 0 )
System.out.print(
( char )( 'A' + arr[j] - 1 ));
}
System.out.println();
}
// Driver code
public static void main(String[] args)
{
printString( 26 );
printString( 51 );
printString( 52 );
printString( 80 );
printString( 676 );
printString( 702 );
printString( 705 );
}
}
// This code is contributed by amal kumar choubey


Python3

def printString(n):
arr = [ 0 ] * 10000
i = 0
# Step 1: Converting to number
# assuming 0 in number system
while (n > 0 ):
arr[i] = n % 26
n = int (n / / 26 )
i + = 1
#Step 2: Getting rid of 0, as 0 is
# not part of number system
for j in range ( 0 , i - 1 ):
if (arr[j] < = 0 ):
arr[j] + = 26
arr[j + 1 ] = arr[j + 1 ] - 1
for j in range (i, - 1 , - 1 ):
if (arr[j] > 0 ):
print ( chr ( ord ( 'A' ) +
(arr[j] - 1 )), end = "");
print ();
# Driver code
if __name__ = = '__main__' :
printString( 26 );
printString( 51 );
printString( 52 );
printString( 80 );
printString( 676 );
printString( 702 );
printString( 705 );
# This code is contributed by Princi Singh


C#

using System;
class GFG{
static void printString( int n)
{
int []arr = new int [10000];
int i = 0;
// Step 1: Converting to
// number assuming 0 in
// number system
while (n > 0)
{
arr[i] = n % 26;
n = n / 26;
i++;
}
// Step 2: Getting rid of 0,
// as 0 is not part of number
// system
for ( int j = 0; j < i - 1; j++)
{
if (arr[j] <= 0)
{
arr[j] += 26;
arr[j + 1] = arr[j + 1] - 1;
}
}
for ( int j = i; j >= 0; j--)
{
if (arr[j] > 0)
Console.Write(( char )( 'A' +
arr[j] - 1));
}
Console.WriteLine();
}
// Driver code
public static void Main(String[] args)
{
printString(26);
printString(51);
printString(52);
printString(80);
printString(676);
printString(702);
printString(705);
}
}
// This code is contributed by 29AjayKumar


Javascript

<script>
function printString(n){
let arr = [];
let i = 0;
// Step 1: Converting to number assuming
// 0 in number system
while (n) {
arr[i] = n % 26;
n = Math.floor(n / 26);
i++;
}
// Step 2: Getting rid of 0, as 0 is
// not part of number system
for (let j = 0; j < i - 1; j++) {
if (arr[j] <= 0) {
arr[j] += 26;
arr[j + 1] = arr[j + 1] - 1;
}
}
let ans = '' ;
for (let j = i; j >= 0; j--) {
if (arr[j] > 0)
ans += String.fromCharCode(65 + arr[j] - 1);
}
document.write(ans + "<br>" );
}
// Driver program to test above function
printString(26);
printString(51);
printString(52);
printString(80);
printString(676);
printString(702);
printString(705);
</script>


输出

ZAYAZCBYZZZAAC

方法3:

我们可以使用递归函数,这无疑减少了时间,提高了效率:

字母表是按顺序排列的,比如:“ABCDEFGHIJKLMNOPQRSTUVWXYZ”。在使用excel时,当您看到列和行的编号是按字母顺序进行的时,您已经体验到了这一点。

以下是我如何有目的地思考它是如何安排的逻辑。

(在数学术语中,[a,b]表示从‘a’到‘b’)。

[1,26]=[A,Z](理解为“1”代表“A”,而“26”代表“Z”)。对于[27,52],它将类似于[AA,AZ],对于[57,78],它将是[BA,BZ]

逻辑是每当字母表在26处结束时,按顺序附加一个字母表。

例如,如果数字是’27’,大于’26’,那么我们只需要除以26,余数为1,我们将“1”视为“A”,并且可以递归完成。

我们将使用python来实现这一点。。

算法是:

1.取一个数组,将字母从A到Z排序。(您还可以使用导入字符串和字符串函数来获取大写的“A到Z”。)

2. 如果 数字小于或等于“26”,只需从数组中获取字母并打印即可。

3. 如果 大于26,使用商余数规则,如果余数为零,有两种可能的方法, 如果 商是“1”,简单地从索引[r-1]中去掉字母(’r’是余数), 其他的 从num=(q-1)中调用函数,并在前面追加到字母索引[r-1]中。

4. 如果 余数不等于“0”,调用num=(q)的函数,并在前面追加字母索引[r-1]。

与此相关的代码是:

Python3

# Or you can simply take a string and perform this logic ,no issue i found in here.
alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
# defined a recursive function here.
# if number is less than "26", simply hash out (index-1)
# There are sub possibilities in possibilities,
# 1.if remainder is zero(if quotient is 1 or not 1) and
# 2. if remainder is not zero
def num_hash(num):
if num < 26 :
return alpha[num - 1 ]
else :
q, r = num / / 26 , num % 26
if r = = 0 :
if q = = 1 :
return alpha[r - 1 ]
else :
return num_hash(q - 1 ) + alpha[r - 1 ]
else :
return num_hash(q) + alpha[r - 1 ]
# Calling the function out here and printing the ALphabets
# This code is robust ,work for any positive integer only.
# You can try as much as you want
print (num_hash( 26 ))
print (num_hash( 51 ))
print (num_hash( 52 ))
print (num_hash( 80 ))
print (num_hash( 676 ))
print (num_hash( 702 ))
print (num_hash( 705 ))


输出

ZAYAZCBYZZZAAC

相关文章: 从列标题中查找Excel列号 本文由 卡尔蒂克 。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请发表评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享