HDOJ 5920 Ugly Problem 【模拟】

来源:互联网 发布:淘宝买家怎么修改评价 编辑:程序博客网 时间:2024/06/13 22:44

Ugly Problem

TimeLimit: 2000/1000 MS (Java/Others)    Memory Limit:65536/65536 K (Java/Others)
Total Submission(s): 1154    Accepted Submission(s): 406
Special Judge

Problem Description

Everyone hates ugly problems.

You are given a positive integer. You must represent that number by sum ofpalindromic numbers.

A palindromic number is a positive integer such that if you write out thatinteger as a string in decimal without leading zeros, the string is anpalindrome. 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 testcases.

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



【题意】给出一个大数,让你找到不多于50个回文数,使它们的和为这个大数。


【思路】题目比较简单,但比较考察细节。由于限制了回文数的个数,我们尽量每次要去找比较大的回文数,可以从一个数的中间开始向两头去找,比如144445,应该找到的回文数为144441,可以发现我们对第一次出现两头的数不相等时,取两头较小的那个数,保证找到的回文数小于这个数,取出后两个数相减,用得到的数继续进行操作即可。


不过值得注意的是,对于100000类似的以0结尾的数时,我们考虑直接减1,把1作为取出的回文数,避免出现问题,然后继续操作。


#include <cstdio>#include <cmath>#include <iostream>#include <cstring>#include <algorithm>using namespace std;#define mst(a,b) memset((a),(b),sizeof(a))#define rush() int T;scanf("%d",&T);while(T--)typedef long long ll;const int maxn = 1005;const int mod = 20090717;const int INF = 0x3f3f3f;const double eps = 1e-9;char s[maxn];char ans[55][maxn];int main(){    int cas=1;    rush()    {        mst(ans,0);        scanf("%s",s);        int len=strlen(s);        int cnt=0;        while(len>=1)        {            if(s[len-1]=='0')            {                ans[cnt++][0]='1';                int flagg=0;                for(int i=len-1; i>=0; i--)                {                    int kk=s[i]-'0'-flagg;                    flagg=0;                    if(i==len-1) kk--;                    if(kk<0)                    {                        flagg=1;                        s[i]='9';                    }                    else s[i]=kk+'0';                }                if(s[0]=='0')                {                    for(int i=0;i<len-1;i++)                    {                        s[i]=s[i+1];                    }                    s[len-1]='\0';                    len--;                }                continue;            }            int flag=0;            char temp[maxn];            temp[len/2]=s[len/2];            for(int i=len/2-1; i>=0; i--)            {                if(s[i]!=s[len-1-i]&&flag==0)                {                    flag=1;                    temp[i]=temp[len-1-i]=min(s[i],s[len-1-i]);                }                else                {                    temp[i]=temp[len-1-i]=s[i];                }            }            temp[len]='\0';            if(flag==0)            {                strcpy(ans[cnt++],temp);                break;            }            char cha[maxn];            int flagg=0;            for(int i=len-1; i>=0; i--)            {                int x1=s[i]-'0',x2=temp[i]-'0';                int x3=(x1-x2-flagg+10)%10;                cha[i]='0'+x3;                flagg=(s[i]-temp[i]-flagg)<0;            }            cha[len]='\0';            int pos;            for(int i=0;i<len;i++)            {                if(cha[i]!='0')                {                    pos=i;                    break;                }            }            strcpy(s,cha+pos);            strcpy(ans[cnt++],temp);            len=strlen(s);        }        printf("Case #%d:\n%d\n",cas++,cnt);        for(int i=0;i<cnt;i++)        {            printf("%s\n",ans[i]);        }    }    return 0;}


原创粉丝点击