이 질문이 스레드 여기 Programming Riddle: How might you translate an Excel column name to a number?quot를 1 씩 반복해서 뺄 필요가있는 이유는 무엇입니까?
을 기반으로하는 엑셀 열 이름 난 철저하게이 코드를 테스트
public String getColName (int colNum) {
String res = "";
int quot = colNum;
int rem;
/*1. Subtract one from number.
*2. Save the mod 26 value.
*3. Divide the number by 26, save result.
*4. Convert the remainder to a letter.
*5. Repeat until the number is zero.
*6. Return that bitch...
*/
while(quot > 0)
{
quot = quot - 1;
rem = quot % 26;
quot = quot/26;
//cast to a char and add to the beginning of the string
//add 97 to convert to the correct ascii number
res = (char)(rem+97) + res;
}
return res;
}
에 열 번호를 번역하는 그 질문의 코드이며, 그것은 작동하지만 난 질문이 있습니다 어떤이 라인이 필요로하는 것은 나의 이해에서
quot = quot - 1;
작업이 반복되는 대한 quot 거리 '는'거리에있는 COL 번호를 매핑하는 데 필요합니다. 즉, 1은 'a'에서 0 거리, 'a'에서 2 ~ 1 거리만큼 떨어져 있어야합니다. 그러나 이것을 설명하기 위해 이것을 한 번 뺄 필요가 없습니까? 루프에 없습니다 결국,
quot = quot/26;
루프를 중지합니다.
아마도 원래 값은 0..25가 아닌 1..26의 값을 결합하여 형성되었을 것입니다. –
하지만이 작업을 수행하기 때문에 0 ... 25입니다. rem + 97, 'a'에서 멀리 떨어지십시오. – committedandroider