Sunday 17 July 2016

Excel Column Title

/*  Excel 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
*/

 #include<iostream>  
 #include<string.h>  
 #include<cmath>  
 using namespace std;  
 char* colTitle(int N){  
   int i = 0, j = 0;  
   char *s = new char[100];  
   char *res;  
   int t;  
   while(N > 0){  
     if(N % 26 == 0){  
       s[i++] = 'Z';  
       N = N / 26 - 1;  
     }  
     else{  
       t = ((N % 26) - 1 + 65);  
       s[i++] = t;  
       N = N / 26;  
     }  
   }  
   res = new char[i];  
   for(int t = i - 1; t >= 0; t--){  
     res[j++] = s[t];  
   }  
   res[j++] = '\0';  
   return res;  
 }  
 int main(){  
   char *s = new char[100];  
   s = colTitle(705);  
   for(int i = 0; s[i]!= '\0'; i++){  
     cout << s[i];  
   }  
   return 0;  
 }  
Share:

0 comments:

Post a Comment

Contact Me

Name

Email *

Message *

Popular Posts

Blog Archive