Cho số nguyên dương n, chuyển thành tiêu đề cột Excel: 1→A, 26→Z, 27→AA, 28→AB...
Ví dụ: 28 → "AB".
Giải thuật
Giống chuyển cơ số 26 nhưng không có chữ số 0. Lặp: n--, lấy n%26 + 'A', n/=26, đảo chuỗi.
Ví dụ triển khai C++
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
string convertToTitle(int n) {
string res;
while (n) {
n--;
res.push_back('A' + n % 26);
n /= 26;
}
reverse(res.begin(), res.end());
return res;
}
};
int main() {
Solution ob;
cout << ob.convertToTitle(30); // AD
return 0;
}
Đầu vào / Đầu ra
Input: 30 Output: AD