forked from xiaoyili/ITint5
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path023_Excel数转换.cpp
More file actions
38 lines (33 loc) · 1.01 KB
/
023_Excel数转换.cpp
File metadata and controls
38 lines (33 loc) · 1.01 KB
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
/*
作者: Annie Kim, anniekim.pku[at]gmail.com
时间: Sep 19, 2013
题目: Excel数转换
难度: Easy
链接: http://www.itint5.com/oj/#23
问题:
Excel中的行列数用A~Z 26个字母表示,A, B, C, D, …, Z, AA, AB, …, AZ, BA, BB, …
分别表示10进制数1, 2, 3, 4, …, 26, 27, 28, …, 52, 53, 54…。
请实现2个函数decToExcel和excelToDec,将10进制数转换为Excel数,以及将Excel数转换为10进制数。
Solution: ...
*/
#include <algorithm>
using namespace std;
const int BASE = 26;
//将十进制数转换为excel数
string decToExcel(int decNum) {
string res;
while (decNum) {
decNum--;
res.push_back(decNum % BASE + 'A');
decNum /= BASE;
}
reverse(res.begin(), res.end());
return res;
}
//将excel数转换为十进制数
int excelToDec(string excelNum) {
int res = 0, N = excelNum.size();
for (int i = 0; i < N; ++i)
res = res * BASE + (excelNum[i] - 'A' + 1);
return res;
}