-
Notifications
You must be signed in to change notification settings - Fork 0
/
Spreadsheets.cpp
82 lines (66 loc) · 1.58 KB
/
Spreadsheets.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
70
71
72
73
74
75
76
77
78
79
80
81
82
#include<iostream>
#include<string>
using namespace std;
void cell1tocell2(const string & str){
int n = str.size();
int r = 0, c = 0;
int i,j;
for(i = 1 ; i < n; ++i){
if(str[i] >= '0' && str[i] <= '9')
r = r * 10 + (str[i] - '0');
else
break;
}
for(j = i + 1; j < n; ++j){
if(str[j] >= '0' && str[j] <= '9')
c = c * 10 + (str[j] - '0');
else{
break;
}
}
string col;
while(c>0){
c -= 1;
col.push_back((char)(c%26 + 'A'));
c /= 26;
}
//for(i = col.size() -1; i >=0; --i) cout << col[i];
reverse(col.begin(),col.end()), cout << col;
cout << r << endl;
}
void cell2tocell1(const string & str){
int n = str.size();
int r = 0, c = 0;
int i;
for(i = 0 ; i < n; ++i){
if(str[i] >= '0' && str[i] <= '9')
r = r * 10 + (str[i] - '0');
else
c = c * 26 + (str[i] - 'A') + 1;
}
cout << 'R';
cout << r;
cout << 'C';
cout << c << endl;
}
int main(){
int n;
cin >> n;
while(n--){
string str;
cin >> str;
bool iscell1 = false;
for(int i = str.size()-1; i >= 0; --i){
if(isalpha(str[i]) && i > 0 && isdigit(str[i-1]))
{
iscell1 = true;
break;
}
}
if(iscell1)
cell1tocell2(str);
else
cell2tocell1(str);
}
return 0;
}