进制转化问题
- 年号字串
小明用字母 A 对应数字 1,B 对应 2,以此类推,用 Z 对应 26。对于 27 以上的数字,小明用两位或更长位的字符串来对应,例如 AA 对应 27,AB 对 应 28,AZ 对应 52,LQ 对应 329。
请问 2019 对应的字符串是什么?
思路1:借助栈进行进制转换(27进制)
#include<iostream>
#include<stack>
using namespace std;
int main() {
int n = 2019;
stack<int> s;
int temp;
while (n) {
temp = n % 26;
s.push(temp);
n /= 26;
}
while (!s.empty()) {
char c = s.top() + 64;
cout << c;
s.pop();
}
}
思路2:暴力,找到三个字母’x’,’y’,’z’,使得 x2626 + y26 + z = 2019
#include<iostream>
#include<stack>
using namespace std;
int main() {
for (char a = 'A'; a <= 'Z'; a++) {
for (char b = 'A'; b <= 'Z';