D - Ugly Problem HDU - 5920

Posted cautx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了D - Ugly Problem HDU - 5920相关的知识,希望对你有一定的参考价值。

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.

InputIn 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).OutputFor 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

        
 

OJ-ID:
hdu-5920

author:
Caution_X

date of submission:
20191029

tags:
模拟

description modelling:
给定一个数n,把n拆成若干个数相加,且这若干个数是回文串
输出:输出拆成了多少个数以及每一个数的大小

major steps to solve it:
把一个数随机拆成两个回文串显然十分不现实。
我们可以把一个按照它所能拆的最大回文串来拆:n=n1(回文串)+n2
若n2不是回文串,n2代替n的位置继续拆出新数,若n2也是回文串,结束,输出答案。

warnings:
n=10特判

AC code:

#include<bits/stdc++.h>
using namespace std;
char num[1005],sub[1005];
char ans[55][1005];
char one[2]="1";
bool judge(char *s){//判断当前数字是否为回文数字
   int lens = strlen(s);
   for(int i = 0;i<lens/2;i++){
        if(s[i]!=s[lens - i - 1]){
            return false;
        }
   }
   return true;
}
void decrease(char *s1,char *s2)
{
    int len1=strlen(s1);
    int len2=strlen(s2);
    int i=len1-1,j=len2-1,flag=0;
    while(i>=0&&j>=0) {
        if(s1[i]-0-flag>=0) {
            s1[i]-=flag;
            flag=0;
        }
        else {
            s1[i] = s1[i] + 10 - flag;
            flag = 1;
        }
        if(s1[i]>=s2[j]) {
            s1[i]=s1[i]-s2[j]+0;
        }
        else {
            s1[i]=s1[i]+10-s2[j]+0;
            flag=1;
        }
        i--,j--;
    }
    while(i>=0&&flag) {
        if(s1[i]-0>=flag) {
            s1[i]-=flag;
            flag=0;
        }
        else {
            s1[i]=s1[i]+10-flag;
            flag=1;
        }
        i--;
    }
    int id=0;
    bool pre_0=true;
    char tmp[1005];
    memset(tmp,0,sizeof(tmp));
    for(int k=0;k<len1;k++) {
        if(s1[k]==0&&pre_0)    continue;
        if(s1[k]!=0&&pre_0) pre_0=false;
        tmp[id++]=s1[k]; 
    }
    if(!id) {
        tmp[id++]=0;
    }
    tmp[id]=;
    strcpy(s1,tmp);
}
void palindromic(char *s1,char *s2)
{
    int len1=strlen(s1),len2;
    if(len1==2&&s1[0]==1&&s1[1]==0){
        s2[0]=9;
        s2[1]=;
        return;
    }
    if(len1&1) len2=len1/2+1;
    else len2=len1/2;
    for(int i=0;i<len2;i++) {
        s2[i]=s1[i];
    }
    s2[len2]=;
    decrease(s2,one);
    if(s2[0]==0) {
        s2[0]=1;
    }
    for(int i=len1-1,j=0;j<i;j++,i--) {
        s2[i]=s2[j];
    }
    s2[len1]=;
}
int main()
{
    //freopen("input.txt","r",stdin);
    int T,kase=1;
    scanf("%d",&T);
    while(T--) {
        scanf("%s",num);
        int id=0;
        while(num[0]!=0&&id<50) {
            if(judge(num)) {
                strcpy(ans[id++],num);
                break;
            }
            memset(sub,0,sizeof(sub)); 
            palindromic(num,sub);
            strcpy(ans[id++],sub);
            decrease(num,sub);
        }
        printf("Case #%d:
%d
",kase++,id);
        for(int i=0;i<id;i++) {
            printf("%s
",ans[i]);
        }
    }
}

 

以上是关于D - Ugly Problem HDU - 5920的主要内容,如果未能解决你的问题,请参考以下文章

HDU - 5920 Ugly Problem 求解第一个小于n的回文数

HDU 5920 Ugly Problem 模拟 (2016中国大学生程序设计竞赛(长春))

POJ 3923 &amp; HDU 2487 Ugly Windows(模拟)

Ugly Problem

高精度 JAVAHDU 5920 Ugly Problem

2016中国大学生程序设计竞赛(长春)-重现赛 1010Ugly Problem 回文数 模拟