Problem Description
Everyone hates ugly problems.
You are given a positive integer. You must represent that number by sum of palindromic numbers.
A palindromic number is a positive integer such that if you write out that integer as a string in decimal without leading zeros, the string is an palindrome. For example, 1 is a palindromic number and 10 is not.
You are given a positive integer. You must represent that number by sum of palindromic numbers.
A palindromic number is a positive integer such that if you write out that integer as a string in decimal without leading zeros, the string is an palindrome. For example, 1 is a palindromic number and 10 is not.
Input
In the first line of input, there is an integer T denoting the number of test cases.
For each test case, there is only one line describing the given integer s ( 1≤s≤101000 ).
For each test case, there is only one line describing the given integer s ( 1≤s≤101000 ).
Output
For each test case, output “Case #x:” on the first line where x is the number of that test case starting from 1. Then output the number of palindromic numbers you used, n, on one line. n must be no more than 50. en output n lines, each containing one of your palindromic numbers. Their sum must be exactly s.
Sample Input
2 18 1000000000000
Sample Output
Case #1: 2 9 9 Case #2: 2 999999999999 1Hint9 + 9 = 18 999999999999 + 1 = 1000000000000
每次减去可以减的最大的,可以使得数位减半
因为只有1000位长度,因此该方法可以在50次之内构造出答案
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
using namespace std;
string xt[101];
int a[10001],b[10001];
int main()
{
int T,k=0;
scanf("%d",&T);
while(T>0)
{
T--;
k++;
string x;
cin>>x;
int i,j;
int len=x.size();
for(i=0;i<len;i++)
a[i+1]=x[i]-'0';
int ss=0,d=1;
while(d<=len)
{
for(i=d;i<=(len+d)/2;i++)
{
b[i]=a[i];
b[len+d-i]=a[i];
}
bool flag=false;
for(i=(len+d)/2+1;i<=len;i++)
{
if(b[i]>a[i])
{
flag=true;
break;
}
else if(b[i]<a[i])
break;
}
if(flag)
{
j=(len+d)/2;
while(b[j]==0)
{
b[j]=9;
b[len+d-j]=9;
j--;
}
b[j]--;
b[len+d-j]--;
if(b[j]==0&&j==d)
{
for(i=d+1;i<=len;i++)
b[i]=9;
}
}
ss++;
xt[ss]="";
flag=false;
for(i=d;i<=len;i++)
{
if(!flag&&b[i]==0)
continue;
flag=true;
xt[ss]+=(b[i]+'0');
}
for(i=len;i>=d;i--)
{
a[i]=a[i]-b[i];
if(a[i]<0)
{
a[i]+=10;
a[i-1]--;
}
}
while(a[d]==0)
d++;
}
printf("Case #%d:\n%d\n",k,ss);
for(i=1;i<=ss;i++)
cout<<xt[i]<<endl;
}
return 0;
}