fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int power(int i){
  5.  
  6. if(i==0)
  7. return 1;
  8. if(i == 1) return 26;
  9.  
  10. else{
  11. int temp = power(i/2);
  12. if(i%2){
  13. return 26 * temp *temp;
  14. }
  15. else
  16. return temp * temp;
  17. }
  18. }
  19. void excel_column(char *s){
  20.  
  21. //AB = 2 + 26 ^1 = 28
  22. //AAB = 2 + 26^1 + 26^2 = 704
  23.  
  24. // Value of B = 'B' -'A' + 1 =2;
  25. // B = 2
  26. // BB = 2 + 2 * (26^1) = 54
  27. // ABB = 2 + 2* (26^1) + 1 *( 26^2) = 730
  28. // C = 3
  29. // AC = 3 * (26^0) + 1 * (26^1) = 29
  30. // ACC = 3 * (26^0) + 3 * (26^1) + 1 * (26^2)=757
  31.  
  32. int i ;
  33. int len = strlen(s);
  34. int rank =0;
  35. for(i=0; i<len;i++){
  36. rank += power(len-i-1) * ((s[i]-'A')+1);
  37. }
  38. printf("%d", rank);
  39. }
  40.  
  41. int main(void) {
  42. // your code goes here
  43. excel_column("AAA");
  44. return 0;
  45. }
  46.  
Success #stdin #stdout 0s 2292KB
stdin
Standard input is empty
stdout
703