08-14-25
This commit is contained in:
52
Luogu/P1015 [NOIP 1999 普及组] 回文数.cpp
Normal file
52
Luogu/P1015 [NOIP 1999 普及组] 回文数.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
|
||||
int charToInt(char c) {
|
||||
if (c >= '0' && c <= '9') return c - '0';
|
||||
if (c >= 'A' && c <= 'Z') return c - 'A' + 10;
|
||||
if (c >= 'a' && c <= 'z') return c - 'a' + 10;
|
||||
return 0;
|
||||
}
|
||||
|
||||
char intToChar(int x) {
|
||||
if (x < 10) return '0' + x;
|
||||
return 'A' + (x - 10);
|
||||
}
|
||||
|
||||
string add(const string& a, int jz) {
|
||||
string b(a);
|
||||
reverse(b.begin(), b.end());
|
||||
string tmp(a.size() + 1, '0');
|
||||
int carry = 0;
|
||||
for (size_t i = 0; i < a.size(); i++) {
|
||||
int ia = charToInt(a[i]);
|
||||
int ib = charToInt(b[i]);
|
||||
int sum = ia + ib + carry;
|
||||
carry = sum / jz;
|
||||
tmp[i] = intToChar(sum % jz);
|
||||
}
|
||||
if (carry > 0) tmp[a.size()] = intToChar(carry);
|
||||
else tmp.pop_back();
|
||||
return tmp;
|
||||
}
|
||||
|
||||
bool check(const string& x) {
|
||||
return equal(x.begin(), x.begin() + x.size()/2, x.rbegin());
|
||||
}
|
||||
|
||||
int main(){
|
||||
int jz;
|
||||
string n;
|
||||
cin >> jz >> n;
|
||||
for (int i = 0; i <= 30; i++) {
|
||||
if (check(n)) {
|
||||
cout << "STEP=" << i << endl;
|
||||
return 0;
|
||||
}
|
||||
n = add(n, jz);
|
||||
}
|
||||
cout << "Impossible!" << endl;
|
||||
}
|
||||
|
Reference in New Issue
Block a user