问题来源:https://leetcode.com/problems/excel-sheet-column-title/
/**
*
* <p>
* ClassName ExcelSheetColumnTitle
* </p>
* <p>
* Description Given a positive integer, return its corresponding column title as appear in an Excel sheet.
*
* For example:
*
* 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB
* </p>
*
* @author TKPad wangx89@126.com
* <p>
* Date 2015年3月25日 下午10:37:50
* </p>
* @version V1.0.0
*
*/
public class ExcelSheetColumnTitle {
public String convertToTitle(int n) {
String s = "";
while (n > 0) {
if (n % 26 == 0) {// 代表一轮循环,那么要将其加上A所对应的整型
s = (char) (n % 26 + 26 - 1 + 65) + s;
n = n / 26 - 1;// 要多减去1
} else {
s = (char) (n % 26 - 1 + 65) + s;
n = n / 26;
}
}
return s;
}
public static void main(String[] args) {
// String convertToTitle = new ExcelSheetColumnTitle().convertToTitle(1406);
// String convertToTitle = new ExcelSheetColumnTitle().convertToTitle(54);
// String convertToTitle = new ExcelSheetColumnTitle().convertToTitle(28);
// String convertToTitle = new ExcelSheetColumnTitle().convertToTitle(1406);
String convertToTitle = new ExcelSheetColumnTitle().convertToTitle(52);
System.out.println(convertToTitle);
}
}