#include<iostream>
#include<cmath>
#include<string.h>
#include<algorithm>
#include<iomanip>
#include<cstring>
#include<map>
#include<vector>
#include<queue>
#include <cctype>
#include<functional>
#include<memory.h>
#include<stack>
#include <cassert>
#include<set>
#include<stdio.h>
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int MAXN = 5000;
const int mod = 10007;
const ll INF =1e15;
int n, m,L,cnt=0;
int x[MAXN << 2];
string sub(string str1, string str2)//高精度减法
{
string str;
int tmp = str1.length() - str2.length();
int cf = 0;
for (int i = str2.length() - 1;i >= 0;i--)
{
if (str1[tmp + i] < str2[i] + cf)
{
str = char(str1[tmp + i] - str2[i] - cf + '0' + 10) + str;
cf = 1;
}
else
{
str = char(str1[tmp + i] - str2[i] - cf + '0') + str;
cf = 0;
}
}
for (int i = tmp - 1;i >= 0;i--)
{
if (str1[i] - cf >= '0')
{
str = char(str1[i] - cf) + str;
cf = 0;
}
else
{
str = char(str1[i] - cf + 10) + str;
cf = 1;
}
}
str.erase(0, str.find_first_not_of('0'));//去除结果中多余的前导0
return str;
}
string getsubs(string s) {
//这个会得到三位数以上的数的以下的最大回文数,二位数和一位数不会得到,因为二位数与一位数,暴力都可以。
int len = s.length();
if (len == 1)return s;
int left, right;
if (len % 2) {
left = len / 2;
right = left;
}
else {
left = len / 2-1;
right = left+1;
}
string ans = "";
bool flag = false;
int pos = -1;
for (;right < len && left >= 0;right++, left--) {
if (flag == false && left == 0) {
string ans = "";
if (s[left] <= s[right]) {
ans += s[left];
for (int i = 0;i < s.length() - 2;i++) {
ans += '0';
}
ans += s[left];
}
else {
for (int i = 0;i < s.length() - 1;i++) {
ans += '9';
}
}
return ans;
}
if (s[right] == s[left]) {
ans += s[right];
if(flag==false)
if (s[right] != '0') {
flag = true;
pos = ans.length() - 1;
}
}
else if (s[right] > s[left]) {
string str = s.substr(0, left+1);
string tmp = str;
reverse(tmp.begin(), tmp.end());
ans += tmp;
if (len % 2) {
tmp = ans.substr(1, ans.length() - 1);
reverse(tmp.begin(),tmp.end());
ans = tmp + ans;
return ans;
}
else {
string tmp2 = ans;
reverse(tmp2.begin(), tmp2.end());
ans = tmp2 + ans;
return ans;
}
}
else {
if (pos != -1) {
ans[pos] = ans[pos] - 1;
ans += s[left];
}
else {
ans[ans.length() - 1] = '9';
ans += s[left] - 1;
}
string str = s.substr(0, left);
string tmp = str;
reverse(tmp.begin(), tmp.end());
ans += tmp;
if (len % 2) {
tmp = ans.substr(1, ans.length() - 1);
reverse(tmp.begin(), tmp.end());
ans = tmp + ans;
return ans;
}
else {
tmp = ans;
reverse(tmp.begin(), tmp.end());
ans = tmp + ans;
return ans;
}
}
if (left == 0) {
if (len % 2) {
string str = ans.substr(1, ans.length() - 1);
reverse(str.begin(), str.end());
ans = str + ans;
return ans;
}
else {
string str = ans;
reverse(str.begin(), str.end());
ans = str + ans;
return ans;
}
}
}
}
int main() {
int t;
cin >> t;
for(int i=1;i<=t;i++){
string s;
cin >> s;
string res[51];
int cnt = 0;
while (s != ""&&s!="0") {
string a = getsubs(s);
res[++cnt] = a;
s = sub(s, a);
}
cout << "Case #" << i << ":" << endl;
cout << cnt << endl;
for (int j = 1;j <= cnt;j++) {
cout << res[j] << endl;
}
}
}
hdu5920 Ugly Problem
最新推荐文章于 2024-09-12 13:19:42 发布