牛客 QQ3 编码
概述
https://www.nowcoder.com/practice/6fc8716ee33e4cc59d58d7e18712094e
递归
两点注意:
- return 那里要 + 1
- 最终我们的结果会偏移一位,因此最终输出的时候 -1
#include <iostream>
#include <string>
using namespace std;
int s1 = 1;
int s2 = s1 + 25;
int s3 = s2 + 25 * 25;
int s4 = s3 + 25 * 25 * 25;
int step [4] = {s4, s3, s2, s1};
int helper(string& s, int i) {
if (i == s.size()) return 0;
int start = (s[i] - 'a') * step[i];
return start + 1 + helper(s, i + 1);
}
int main() {
string s;
cin >> s;
cout << helper(s, 0) - 1;
return 0;
}
Links: 牛客-qq03-编码