1496 普通回文数 PAT1019
作者:
NikoNairre
,
2023-11-19 16:04:16
,
所有人可见
,
阅读 87
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
vector<string> str;
vector<string> num_str(int n, int rdx)
{
if (n == 0) return vector<string>(1, "0");
vector<string> res;
while (n) {
int q = n / rdx;
res.push_back(to_string(n % rdx));
n = q;
}
return res; //reversed
}
int main()
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int n, b; cin >> n >> b;
//b can be very huge, so for the num in b-radix, a single digit might include several chars
//eg: N = 114514, b = 1919810, the N in b-radix is 114514, but it is a single digit
//so we use vector<string> to store a num instead of traditional string
vector<string> base = num_str(n, b); //the result will be reversed order
int k = 0; //k checks from tail to head
bool match = true;
for (int i = base.size() - 1; i >= 0; i-- ) { //i checks from head to tail
if (base[i] != base[k]) { //every part must be matched
match = false;
break;
}
k++ ;
}
if (match) cout << "Yes" << endl;
else cout << "No" << endl;
for (int i = base.size() - 1; i >= 0; i-- ) { //output N in b-radix with natural order
if (i != base.size() - 1) cout << " ";
cout << base[i];
}
return 0;
}