hdu 5920 Ugly Problem 字符串处理

来源:互联网 发布:淘宝网申请退货 编辑:程序博客网 时间:2024/06/17 08:14

Ugly Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1336    Accepted Submission(s): 453
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
2181000000000000
 

Sample Output
Case #1:299Case #2:29999999999991
Hint
9 + 9 = 18999999999999 + 1 = 1000000000000
 

Source
2016中国大学生程序设计竞赛(长春)-重现赛  
#include<iostream>#include<algorithm>using namespace std;const int N = 1010;char a[N],b[N],c[N],ans[N][N];int main(){    int T,cas = 1;    scanf("%d",&T);    while(T--)    {        scanf("%s",a);        int num = 0,len = strlen(a);        while(len >= 1)        {            //获取回文数字b            int flag = 0;            b[len >> 1] = a[len >> 1];            for(int i = len / 2 - 1; i >= 0; i--)            {                if(a[i] != a[len - 1 - i] && !flag)//第一次找到不对称的地方                {                    flag = 1;                    b[i] = b[len - 1 - i] = min(a[i],a[len - 1 - i]);                }                else                    b[i] = b[len - 1 - i] = a[i];            }            b[len] = 0;            //特判            if(!flag)//a已经是回文串            {                strcpy(ans[num++],a);                break;            }            if(b[0] == '0')//特判            {                if(a[0] == '1')//分成1和999999.....                {                    b[0] = '1',b[1] = 0;                    strcpy(ans[num++],b);                    for(int i = 0; i < len - 1; i++)                        b[i] = '9';                    b[len - 1] = 0;                    strcpy(ans[num++],b);                }                else//分成9+199..991,8 + 299..992,7 + 399..993等                {                    b[0] = b[len - 1] = (char)(a[0] - 1);                    for(int i = 1; i < len - 1; i++)                        b[i] = '9';                    b[len] = 0;                    strcpy(ans[num++],b);                    int t6 = 11 - (int)(a[0] - '0');                    b[0] = (char)(t6 + '0');                    b[1] = 0;                    strcpy(ans[num++],b);                }                if((len & 1) && (a[len >> 1] != '0'))//奇数位中间位置可能不为0,需要再次计算                {                    strcpy(a,a + len/2);                    len = (len + 1) >> 1;                    continue;                }                break;            }            // 获取c,c = a - b            int t4 = 0;            for(int i = len - 1; i >= 0; i--)            {                int t1 = a[i] - '0', t2 = b[i] - '0';                int t3 = (t1 - t2 - t4 + 10) % 10;                c[i] = (char)(t3 + '0');                t4 = (int)((a[i] - b[i] - t4) < 0);            }            c[len] = 0;            //清除c的前导零            int t5;            for(int i = 0; i < len; i++)            {                if(c[i] != '0')                {                    t5 = i;                    break;                }            }            strcpy(ans[num++],b);            strcpy(a,c + t5);//将c赋值给a继续下一次循环            len = strlen(a);        }        printf("Case #%d:\n",cas++);        printf("%d\n",num);        for(int i = 0; i < num; i++)            printf("%s\n",ans[i]);    }    return 0;}


原创粉丝点击