HDU - 5920- Ugly Problem (字符串模拟&&思路)

30 篇文章 0 订阅
17 篇文章 0 订阅

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

题意:

给出个炒鸡大的数,把他用50个以内的回文数之和表示。

思路:

//思路:设a为给的数
while(a>=10)
{
    x=a;//取前一半
    x--;
    x+= reverse(x);
    a=a-x;
}

Solution:

#include <bits/stdc++.h>
using namespace std;
const int  maxn = 1e3+10;
bool getPal(char *s)//
{
    int n=strlen(s);
    if(n==1) return 1;
    if(n==2&&s[0]=='1')
    {
        s[0]='9';
        s[1]=0;
        return 0;
    }
    s[(n+1)/2-1]--;
    for(int i=(n+1)/2-1; i>0; i--) //减一可能会有小于零的情况,模拟减法
    {
        if(s[i]<'0')
            s[i]+=10,s[i-1]--;
    }
    if(s[0]=='0')               //如果首位减没了,那么全部进一位,这是总的位数少1
    {
        for(int i=0; i<n; i++)
        {
            s[i]=s[i+1];
        }
        n--;
    }
    for(int i=(n+1)/2; i<n; i++)
        s[i]=s[n-i-1];
    return 0;
}
bool subStr(char *a,char *b)//算a-b,结果存入a  其实就是从s中减去构造的回文串
{
    int na = strlen(a);
    int nb = strlen(b);
    for(int i=na-1,j=nb-1; j>=0; i--,j--)
    {
        if(a[i]>=b[j]) a[i] = a[i]-b[j]+'0';
        else
            a[i]=a[i]+10-b[j]+'0',a[i-1]--;
    }
    int p;
    for(p=0; a[p]=='0'; p++)
    {
        if(p==na)
            return 1; //刚好减完,说明相等
    }
    for(int i=0; p<=na; i++,p++)
        a[i]=a[p];
    return 0;
}
char s[maxn],ans[50][maxn];
int cnt;
int main()
{
    ios::sync_with_stdio(false);
    int t;
    cin>>t;
    for(int cas=1; cas<=t; cas++)
    {
        cin>>s;
        for(cnt=0;; cnt++)
        {
            strcpy(ans[cnt],s);
            if(getPal(ans[cnt])||subStr(s,ans[cnt]))
                break;
        }
        printf("Case #%d:\n%d\n",cas,cnt+1);
        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、付费专栏及课程。

余额充值