-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexcel_col_to_int_int_to_col.cpp
61 lines (49 loc) · 1.11 KB
/
excel_col_to_int_int_to_col.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
//Excel column to number and vice a versa
//A - 0, B - 1, ...
//https://ide.geeksforgeeks.org/xgInOXFqRp
#include<bits/stdc++.h>
using namespace std;
#define F(a,b,var) for(int var=a;var<b;var++)
#define FAST_INP ios_base::sync_with_stdio(false);cin.tie(NULL)
int colToNumber(string x){
int res = 0;
for(int i=0; i<x.size(); i++){
res *= 26;//if A starts from --> '0' then mod 25 else if A starts from '1'
res += x[i] - 'A' + 1;
}
return res;
}
string numberToCol(int y){
string n = "";
int c = 0;
while(y > 0){
int rem = y%26;cout<<rem<<" ";
if(rem == 0){
n += 'Z';
y = y/26 - 1;
}
else{
n += (rem - 1) + 'A';
y = y/26;
}
c++;
}
n += '\0';
reverse(n.begin(), n.end());
return n;
}
int main()
{
//code
FAST_INP;
int T;
cin>>T;
while(T--)
{
string s; cin>>s;
int x; cin>>x;
int n = s.size();
cout<<colToNumber(s)<<"\n";
cout<<numberToCol(x)<<"\n";
}
}