首页 > 试题广场 >

Excel列名称

[编程题]Excel列名称
  • 热度指数:1708 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解

excel列序号与列名称的映射关系是:
1->A
2->B
...
27->AA
28->AB
...

数据范围:
示例1

输入

5

输出

"E"
示例2

输入

29

输出

"AC"
import java.util.*;

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param n int整型
     * @return string字符串
     */
    public String ExcelTitle (int n) {
        int tens=0;
        while(true) {
            if(n>=Math.pow(26,tens) && n<=Math.pow(26,tens+1)) {
                break;
            }
            tens++;
        }

        StringBuilder sb = new StringBuilder();
        for(int i=0;i<tens+1;i++) {
            int temp=0;
            temp = n / (int)Math.pow(26,tens-i);
            if(temp==0) {
              char tempChar = sb.charAt(i-1);
              sb.deleteCharAt(i-1);
              sb.append((char)(tempChar-1));
              temp= 26;
            }
            sb.append((char)('A'+temp-1));
            n= n-temp*(int)Math.pow(26,tens-i);

        }
         


        return sb.toString();
    }
}



单位考试考了这道题,已经想到A0=ZZ了却没写出来,下了考场才A。很气。大家做题也要注意时刻保持紧张,不要轻敌呀
发表于 2023-11-28 16:07:24 回复(0)
package main
//import "fmt"

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param n int整型 
 * @return string字符串
*/
func ExcelTitle( n int ) string {
    ans:=""
    for n>0{
        x:=n%26
        if x==0{
            ans=string('Z')+ans
            n=n/26-1
        }else{
            ans=string('A'+x-1)+ans
            n/=26
        }
    }
    return ans
}

发表于 2023-03-17 11:03:58 回复(0)
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param n int整型
# @return string字符串
#
class Solution:
    def ExcelTitle(self, n: int) -> str:
        # write code here
        convertStr = ""
        convertDict = {
            1: "A",
            2: "B",
            3: "C",
            4: "D",
            5: "E",
            6: "F",
            7: "G",
            8: "H",
            9: "I",
            10: "J",
            11: "K",
            12: "L",
            13: "M",
            14: "N",
            15: "O",
            16: "P",
            17: "Q",
            18: "R",
            19: "S",
            20: "T",
            21: "U",
            22: "V",
            23: "W",
            24: "X",
            25: "Y",
            0: "Z",
        }
        while int(n / 26) != 0:
            x = convertDict[n % 26]  # 获取末尾数字对应的字母
            convertStr += x  # 将对应的字母添加到临时变量末尾
            if x == "Z":                    # 如果末尾是Z,因为对前一位借位,右移时要减去1
                n = int((n / 26) - 1)
            else:
                n = int(n / 26)  # 右移一位,使右二位移至右一位,以此类推
        x = convertDict[n]
        convertStr += x
        return convertStr[::-1]

发表于 2022-09-28 17:12:39 回复(0)
class Solution:
    def ExcelTitle(self , n: int) -> str:
        # write code here
        import string
        res = ''
        while n:
            n -= 1
            res = string.ascii_uppercase[n%26] + res
            n //= 26
        return res

发表于 2022-07-03 17:02:27 回复(0)
function ExcelTitle(n) {
  let v = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  let z = 0;
  let a = "";

  while (n >= 1) {
    if (n % 26 !== 0) {
      a = v[(n % 26) - 1] + a;
      n = (n - (n % 26)) / 26;
    } else {
      a = v[25] + a;
      n = n / 26 - 1;
    }
  }
  return a;
}

module.exports = {
  ExcelTitle: ExcelTitle,
};

发表于 2022-06-23 23:01:42 回复(0)
function ExcelTitle(n) {
    // write code here
    var char = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    var res = 0;
    var cc = ''
    while (n > 0) {
        var lef = n % 26
        if(lef !==0 ){
            cc = char[lef-1]+ cc
            n = (n - lef)/26
        }else{
            cc = char[25]+ cc
            n = n /26 -1
        }
      
    }
    return cc
}
发表于 2022-04-22 21:48:31 回复(0)
import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param n int整型 
     * @return string字符串
     */
    public String ExcelTitle(int n) {
        char[] chars = new char[] { 'Z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
                                    'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
                                    'Y' };
        // write code here
        StringBuilder sb = new StringBuilder();
        while (n > 0) {
            int temp = n % 26;
            sb.append(chars[temp]);
            n = n / 26;
            if (temp == 0) {
                n = n - 1;
            }
        }
        return sb.reverse().toString();
    }
}

发表于 2022-03-13 23:30:33 回复(0)