题目链接:https://ac.nowcoder.com/acm/problem/16811
可以直接调用string的reverse方法来翻转字符串
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <cstring>
using namespace std;
typedef long long ll;
static const int MAX_N = 1005;
static const ll Mod = 1e9 + 7;
static const int N = 105;
static const ll INF = (ll)1 << 60;
string s1;
int n;
int get_v(char ch) {
if (ch >= 'a') return ch - 'a' + 10;
else return ch - '0';
}
char get_c(int v) {
if (n == 16 && v > 9) return v - 10 + 'a';
else return v + '0';
}
string add_N(string s1, string s2) {
int la = s1.length();
int x = 0;
for (int i = la - 1; i >= 0; --i) {
s1[i] = tolower(s1[i]);
s2[i] = tolower(s2[i]);
int v = get_v(s1[i]) + get_v(s2[i]) + x;
x = v / n;
v %= n;
s1[i] = get_c(v);
}
if (x) s1 = "1" + s1;
return s1;
}
int main() {
/*freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);*/
scanf("%d", &n);
cin >> s1;
int cur = 0;
bool flag = false;
string s2 = s1;
reverse(s2.begin(), s2.end());
if (s2 == s1) flag = true;
for (int i = 1; i <= 30 && !flag; ++i) {
s1 = add_N(s1, s2);
s2 = s1;
reverse(s2.begin(), s2.end());
if (s1 == s2) {
flag = true;
cur = i;
break;
}
}
if (flag) printf("STEP=%d\n", cur);
else puts("Impossible!");
return 0;
}