-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcol_name_from_col_number_in_ms_excel.cpp
More file actions
132 lines (114 loc) · 2.51 KB
/
col_name_from_col_number_in_ms_excel.cpp
File metadata and controls
132 lines (114 loc) · 2.51 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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// new try
int Solution::titleToNumber(string s) {
int ans = 0, n = s.length();
for(int i=n-1; i>=0; i--){
ans += (s[i]-'A'+1)*pow(26, n-i-1);
}
return ans;
}
// old one
int Solution::titleToNumber(string A) {
int result = A[A.length()-1]-'A'+1;
int j=1;
for(int i=A.length()-2; i>=0; i--){
// cout<<i<<"--";
int hash = A[i]-'A';
// cout<<hash+1<<endl;
result = result + pow(26, j)*(hash+1);
j++;
}
return result;
}
// Given a positive integer, return its corresponding column title as appear in an Excel sheet.
// MS Excel columns has a pattern like A, B, C, … ,Z, AA, AB, AC,…. ,AZ, BA, BB, … ZZ, AAA, AAB ….. etc. In other words, column 1 is named as “A”, column 2 as “B”, column 27 as “AA”.
#include <bits/stdc++.h>
using namespace std;
void ms_excel(){
long long int n;
cin>>n;
vector<int> vect;
while(n != 0){
if(n%26 == 0){
cout<<"-1-";
vect.push_back('Z');
}
else{
cout<<"-2-";
vect.push_back((n%26)-1+'A');
}
if(n==26){
n = 0;
}
n = n/26;
}
for(int i=vect.size()-1; i>=0; i--){
cout<<char(vect[i]);
}
cout<<endl;
}
void titleTONumber(){
string A;
// cin.ignore(1);
getline(cin, A);
cout<<A.length()<<"==="<<endl;
int result = A[A.length()-1]-'A'+1;
int j=1;
for(int i=A.length()-2; i>=0; i--){
// cout<<i<<"--";
int hash = A[i]-'A';
// cout<<hash+1<<endl;
result = result + pow(26, j)*(hash+1);
j++;
}
cout<<result<<endl;
}
int Solution::titleToNumber(string A) {
long long int count = 0;
for(int i = 0;i<A.size();i++){
count+= (((int)(A[i]-‘A’)+1)*pow(26,A.size()-1-i));
}
return count;
}
int main()
{
int test;
cin>>test;
cin.ignore(1);
while(test--){
// ms_excel();
titleTONumber();
}
return 0;
}
// ////////////////
// #include<iostream>
// #include<algorithm>
// #include<vector>
// typedef long long int lli;
// using namespace std;
// void col_name(lli n){
// vector<char> col;
// while(n){
// int temp = n % 26;
// if(temp == 0){
// col.push_back('Z');
// n = (n/26)-1;
// }
// else{
// col.push_back((char)(temp-1) + 'A');
// n /= 26;
// }
// }
// reverse(col.begin(), col.end());
// for(int i = 0; i < col.size(); i++){
// cout << col[i];
// }
// cout << endl;
// }
// int main(){
// int t; cin >> t;
// while(t--){
// lli n; cin >> n;
// col_name(n);
// }
// }