问题描述:
Excel单元格的地址表示很有趣,它使用字母来表示列号,比如:
A表示第1列,
B表示第2列,
Z表示第26列,
AA表示第27列,
AB表示第28列,
BA表示第53列,
样例输入
26
样例输出
Z
样例输入
2054
样例输出
BZZ
问题描述:
代码如下:
package easy;
import java.util.Scanner;
public class Number_n {
public static void main(String[] args) {
char[] standard = new char[27];
for(int i = 1;i <=26;i++){
int x = 64+i;
standard[i] = (char)x;
} //将数字i对应的字母填入standard[i]中
int count=0;
Scanner input = new Scanner(System.in);
int x = input.nextInt();
int temp = x;
while(temp>0){
temp = temp/26;
count++; //最终count为多少,Excel地址就有多少位
}
int[] num = new int[count];
for(int i = 0;i < count;i++){
if(x%26==0){
num[i] = 26;
x = x/26 - 1;
}
else{
num[i] = x%26;
x = x/26;
}
}
for(int p = num.length-1;p >=0;p--){
System.out.println(standard[num[p]]);
}
input.close();
}
}
解决思路:
只有可以除尽时下一步的x-1(因为要把所有位的公共26提出来),当除不尽的时候,直接x=x/26,不用-1(因为没有公共的26,故不需要)
注意:
1、常用ASCII码值,熟记:
A-Z:65-90
a-z:97-122
1-9:48-57