LeetCode 168
Excel Sheet Column Title
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
1 /************************************************************************* 2 > File Name: LeetCode168.c 3 > Author: Juntaran 4 > Mail: JuntaranMail@gmail.com 5 > Created Time: Tue 10 May 2016 08:06:44 PM CST 6 ************************************************************************/ 7 8 /************************************************************************* 9 10 Excel Sheet Column Title 11 12 Given a positive integer, 13 return its corresponding column title as appear in an Excel sheet. 14 15 For example: 16 17 1 -> A 18 2 -> B 19 3 -> C 20 ... 21 26 -> Z 22 27 -> AA 23 28 -> AB 24 25 ************************************************************************/ 26 27 #include <stdio.h> 28 29 char* convertToTitle( int n ) 30 { 31 if( n < 1 ) 32 { 33 return ""; 34 } 35 int i; 36 int count = 0; 37 for( i=n-1; i>=0; i=i/26-1 ) 38 { 39 count ++; 40 } 41 char* ret = (char*)malloc(sizeof(char)*count); 42 for( i=n-1; i>=0; i=i/26-1 ) 43 { 44 ret[--count] = i%26 + 'A'; 45 } 46 return ret; 47 } 48 49 int main() 50 { 51 int n = 28; 52 char* ret = convertToTitle(n); 53 printf("%s\n", ret); 54 }