HDU5920-Ugly Problem

100 篇文章 0 订阅

Ugly Problem

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

Recommend
wange2014
 


题意:给你一个数字n,让你把它拆成几个回文数的和,并且回文数的个数不能多于50

解题思路:每次找到一个比较接近n的比n小的回文数a,然后n=n-a


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <map>
#include <bitset>
#include <set>
#include <vector>
#include <functional>

using namespace std;

int n,m;
char ch[100009], s1[100009], s2[100009];
char ans[100][100009];
int cnt,len;

void f(char *a,char *b)
{
    int len1=strlen(a),len2=strlen(b);
    for(int i=0; i<len1/2; i++) swap(a[i],a[len1-1-i]);
    for(int i=0; i<len2/2; i++) swap(b[i],b[len2-1-i]);
    for(int i=len2; i<len1; i++) b[i]='0';
    for(int i=0; i<len1; i++)
    {
        if(a[i]>=b[i]) a[i]=a[i]-b[i]+'0';
        else
        {
            a[i]=10+a[i]-b[i]+'0';
            int k=1;
            while(a[i+k]==0)
            {
                a[i+k]='9';
                k++;
            }
            a[i+k]--;
        }
    }
    int k=len1-1;
    while(a[k]=='0') k--;
    for(int i=0; i<(k+1)/2; i++) swap(a[i],a[k-i]);
    a[k+1] = '\0';
    for(int i=0; i<len2/2; i++) swap(b[i],b[len2-1-i]);
    b[len2] = '\0';
}

void solve()
{
    len = strlen(ch);
    if(len <= 1||!strcmp(ch,"10")||!strcmp(ch,"100"))
    {
        if(len==1) strcpy(ans[cnt++], ch);
        else if(len==2)
        {
            strcpy(ans[cnt++],"9");
            strcpy(ans[cnt++],"1");
        }
        else
        {
            strcpy(ans[cnt++],"99");
            strcpy(ans[cnt++],"1");
        }
        return;
    }
    int l=0, r=len-1;
    int x1=(l+r-1)/2,x2=(l+r)/2+1,flag=0;
    for(int i = x1; i >= 0; i--)
    {
        if(ch[i] >= ch[x2 + x1 - i])
        {
            flag = 1;
            break;
        }
    }
    strcpy(s1, ch);
    if(flag == 1)
    {
        int ll = r - x1 + 1;
        s2[0] = '1';
        for(int i = 1; i < ll; i++) s2[i] = '0';
        s2[ll] = '\0';
        f(s1, s2);
    }
    strcpy(s2, s1);
    int lenl=strlen(s2);
    for(int i = (lenl-1) / 2 + 1; i < lenl; i++)
        s2[i] = s1[lenl - i-1];
    f(ch,s2);
    strcpy(ans[cnt++], s2);
    solve();
}

int main()
{
    int t,cas=0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s", ch);
        cnt = 0;
        solve();
        printf("Case #%d:\n", ++cas);
        printf("%d\n", cnt);
        for(int i = 0; i < cnt; i++) printf("%s\n", ans[i]);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值