牛客训练 16811 回文串 (高精度)

题目链接: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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值