Ugly Problem

时间:2023-03-09 15:47:36
Ugly Problem

Ugly Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Special Judge

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.

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).

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
1
Hint

9 + 9 = 18
999999999999 + 1 = 1000000000000

分析:找一个较大回文数,然后大数相减;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<ll,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
const int maxn=1e3+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t,cnt,cas;
char a[maxn],ans[][maxn];
string gao(string a, string b){
int lena = a.length();
int lenb = b.length();
int len = max(lena, lenb) + ;
char res[len];
memset(res, , sizeof(res));
int flag, reslen = len;
len--;
res[len] = ;
len--;
lena--; lenb--;
flag = ;
while(lenb >= ){
res[len] = a[lena--] - b[lenb--] + '' - flag;
flag = ;
if(res[len] < ''){
flag = ;
res[len] = res[len] + ;
}
len--;
}
while(lena >= ){
res[len] = a[lena--] - flag;
flag = ;
if(res[len] < ''){
flag = ;
res[len] = res[len] + ;
}
len--;
}
while((res[flag] == || res[flag] == '') && flag < reslen) flag++;
if(flag == reslen) return "";
return res + flag;
}
void find(char*p,int now)
{
int i,len=strlen(p);
ans[now][len]=;
for(int i=;i<(len+)/;i++)
ans[now][i]=ans[now][len--i]=p[i];
bool flag=false;
for(int i=len/-;i>=;i--)
{
if(p[i]<p[len--i])break;
else if(p[i]>p[len--i])
{
ans[now][i]--;
ans[now][len--i]--;
for(int j=i+;j<len--i;j++)ans[now][j]=ans[now][len--j]='';
break;
}
}
for(i=;ans[now][i]=='';i++)
{
ans[now][len--i]='';
}
strcpy(ans[now],ans[now]+i);
strcpy(a,gao(p,ans[now]).c_str());
}
int main()
{
int i,j;
scanf("%d",&t);
while(t--)
{
cnt=;
scanf("%s",a);
while(strcmp(a,"")!=)
{
find(a,cnt);
cnt++;
}
printf("Case #%d:\n%d\n",++cas,cnt);
rep(i,,cnt-)printf("%s\n",ans[i]);
}
//system("Pause");
return ;
}