forked from lennylxx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path168.c
More file actions
36 lines (32 loc) · 671 Bytes
/
168.c
File metadata and controls
36 lines (32 loc) · 671 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *convertToTitle(int n) {
char *ans = (char *)calloc(10, sizeof(char));
int i;
i = 0;
while (n > 0) {
int t = n % 26;
if (t == 0) { t = 26; ans[i] = 'Z'; }
else { ans[i] = t + 'A' - 1; }
n -= t;
n /= 26;
i++;
}
/* reverse string */
int j = strlen(ans) - 1;
i = 0;
while (i < j) {
char t = ans[i];
ans[i] = ans[j];
ans[j] = t;
i++;
j--;
}
return ans;
}
int main() {
int n = 26;
printf("%s\n", convertToTitle(n));
return 0;
}