假设我们有一个正整数;我们必须找到其在电子表格中显示的相应列标题。因此[1:A],[2:B],[26:Z],[27:AA],[28:AB]等。
因此,如果输入为29,则输出将为AC。
为了解决这个问题,我们将遵循以下步骤-
当n不为零时,执行-
n:= n − 1
res:= res + n mod 26 +'A'的ASCII
n:= n / 26
反转数组res
返回资源
让我们看下面的实现以更好地理解-
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
string convertToTitle(int n) {
string res;
while(n){
res += (−−n)%26 + 'A';
n /= 26;
}
reverse(res.begin(), res.end());
return res;
}
};
main(){
Solution ob;
cout << (ob.convertToTitle(30));
}30
输出结果
AD