add file
This commit is contained in:
38
src/168.c
Normal file
38
src/168.c
Normal file
@@ -0,0 +1,38 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
char *convertToTitle(int columnNumber)
|
||||
{
|
||||
char *ret = malloc(2);
|
||||
int size = 1;
|
||||
|
||||
while (columnNumber > 0)
|
||||
{
|
||||
columnNumber--;
|
||||
const int mod = columnNumber % 26;
|
||||
columnNumber /= 26;
|
||||
ret[size - 1] = 'A' + mod;
|
||||
|
||||
size++;
|
||||
ret = realloc(ret, size + 1);
|
||||
}
|
||||
|
||||
ret[size - 1] = '\0';
|
||||
|
||||
for (int i = 0, j = size - 2; i < j; i++, j--)
|
||||
{
|
||||
const char tmp = ret[i];
|
||||
ret[i] = ret[j];
|
||||
ret[j] = tmp;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
char *ans = convertToTitle(2147483647);
|
||||
printf("%s\n", ans);
|
||||
free(ans);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user