码迷,mamicode.com
首页 > 其他好文 > 详细

D - Ugly Problem HDU - 5920

时间:2019-10-29 23:36:48      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:strlen   thml   mission   个数   bool   style   结束   flag   lse   

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]=\0;
    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]=\0;
        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]=\0;
    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]=\0;
}
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:\n%d\n",kase++,id);
        for(int i=0;i<id;i++) {
            printf("%s\n",ans[i]);
        }
    }
}

 

D - Ugly Problem HDU - 5920

标签:strlen   thml   mission   个数   bool   style   结束   flag   lse   

原文地址:https://www.cnblogs.com/cautx/p/11762119.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!