hdoj5920Ugly Problem

Ugly Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 476    Accepted Submission(s): 190
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 ( 1s101000 ).
 

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
 

Source
 
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<list>
#include<vector>
#include<set>
using namespace std;
typedef long long LL;
typedef pair<int,int>pii;
const int maxn=10010;
char s[maxn];
char MaxPal[maxn];
char str[55][maxn];
int ans=0;
void rev(char *a){
    int len=strlen(a);
    for(int i=0;i<len/2;++i){
        char temp=a[i];a[i]=a[len-i-1];
        a[len-i-1]=temp;
    }
}
void Sub(char *a,char *b){
    int lena=strlen(a),lenb=strlen(b);
    rev(a);rev(b);int q=0;
    for(int i=lenb;i<=lena;++i){b[i]='0';}
    for(int i=0;i<lena;++i){
        if((a[i]-'0'-q)>=(b[i]-'0')){
            a[i]=((a[i]-'0')-q-(b[i]-'0'))+'0';q=0;
        }
        else {
            a[i]=((a[i]-'0')+10-q-(b[i]-'0'))+'0';q=1;
        }
    }int i;
    for(i=lena-1;i>=0;--i){
        if(a[i]!='0'){a[i+1]=0;break;}
    }rev(a);
}
void Find(char *temp){
    int len=strlen(temp);bool sign=true;
    for(int i=0;i<len/2;++i){
        if(temp[i]!=temp[len-i-1]){
            sign=false;break;
        }
    }
    if(sign){strcpy(MaxPal,temp);return ;}
    if(len&1){
        for(int i=len/2-1;i>=0;--i){
            if(temp[i]!=temp[len/2+len/2-i]){
                if(temp[i]<temp[len/2+len/2-i]){
                    for(int j=0;j<len/2+len/2-i;++j){
                        MaxPal[j]=temp[j];
                    }
                    for(int j=len/2+len/2-i;i>=0;++j,--i){
                        MaxPal[j]=temp[i];
                    }MaxPal[len]=0;
                    return ;
                }
                else {
                    int k;
                    for(k=len/2;k>=0;--k){
                        if(temp[k]!='0'){break;}
                    }
                    for(int j=0;j<k;++j){
                        MaxPal[j]=temp[j];
                    }MaxPal[k]=MaxPal[len-k-1]=temp[k]-1;
                    for(int j=k+1;j<len-k-1;++j){
                        MaxPal[j]='9';
                    }k-=1;
                    for(int j=len-k-1;k>=0;--k,++j){
                        MaxPal[j]=temp[k];
                    }MaxPal[len]=0;
                    if(MaxPal[0]=='0'){
                        for(int j=0;j<len-1;++j){
                            MaxPal[j]='9';
                        }
                        MaxPal[len-1]=0;
                    }
                    return ;
                }
            }
        }
    }
    else {
        for(int i=len/2-1;i>=0;--i){
            if(temp[i]!=temp[len-i-1]){
                if(temp[i]<temp[len-i-1]){
                    for(int j=0;j<len-i-1;++j){
                        MaxPal[j]=temp[j];
                    }
                    for(int j=len-i-1;i>=0;++j,--i){
                        MaxPal[j]=temp[i];
                    }MaxPal[len]=0;
                    return ;
                }
                else {
                    int k;
                    for(k=len/2-1;k>=0;--k){
                        if(temp[k]!='0'){break;}
                    }
                    for(int j=0;j<k;++j){
                        MaxPal[j]=temp[j];
                    }MaxPal[k]=MaxPal[len-k-1]=temp[k]-1;
                    for(int j=k+1;j<len-k-1;++j){
                        MaxPal[j]='9';
                    }k-=1;
                    for(int j=len-k-1;k>=0;--k,++j){
                        MaxPal[j]=temp[k];
                    }MaxPal[len]=0;
                    if(MaxPal[0]=='0'){
                        for(int j=0;j<len-1;++j){
                            MaxPal[j]='9';
                        }MaxPal[len-1]=0;
                    }
                    return ;
                }
            }
        }
    }
}
void Solve(char *temp){
    Find(temp);strcpy(str[ans++],MaxPal);
    if(strcmp(temp,MaxPal)==0)return ;
    Sub(temp,MaxPal);Solve(temp);
}
int main()
{
    int t,Test=1;cin>>t;
    while(t--){
        scanf("%s",s);
        Find(s);
        ans=0;Solve(s);
        printf("Case #%d:\n%d\n",Test++,ans);
        for(int i=0;i<ans;++i){
            printf("%s\n",str[i]);
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值