-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path168.excel表列名称.cpp
69 lines (64 loc) · 1.63 KB
/
168.excel表列名称.cpp
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
* @lc app=leetcode.cn id=168 lang=cpp
*
* [168] Excel表列名称
*/
#include <iostream>
#include <math.h>
#include <algorithm>
using namespace std;
// @lc code=start
class Solution
{
public:
// inline size_t summation(size_t n)
// {
// return (26 * (pow(26, n) - 1) / 25);
// }
// string convertToTitle(int columnNumber)
// {
// size_t tt_len = 0;
// string res;
// if (columnNumber < 26)
// {
// res += ((char)(64 + columnNumber));
// return res;
// }
// while (1)
// {
// if (summation(tt_len) >= static_cast<size_t>(columnNumber))
// break;
// tt_len++;
// }
// columnNumber -= summation(tt_len - 1);
// int dig;
// for (size_t i = 0; i < tt_len; i++)
// {
// if (i == tt_len - 1)
// {
// res += ((char)(64 + columnNumber));
// break;
// }
// if (columnNumber > 0)
// dig = ((columnNumber - 1) / (pow(26, tt_len - i - 1)));
// else
// dig = 0;
// res += (char)(65 + dig);
// columnNumber -= dig * (pow(26, tt_len - i - 1));
// }
// return res;
// }
string convertToTitle(int columnNumber)
{
string ans;
while (columnNumber > 0)
{
int a0 = (columnNumber - 1) % 26 + 1;
ans += a0 - 1 + 'A';
columnNumber = (columnNumber - a0) / 26;
}
reverse(ans.begin(), ans.end());
return ans;
}
};
// @lc code=end