D - Ugly Problem HDU - 5920

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 (1≤s≤101000).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]);
        }
    }
}

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

时间: 2024-11-05 22:42:05

D - Ugly Problem HDU - 5920的相关文章

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

Ugly Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 0    Accepted Submission(s): 0Special Judge Problem Description Everyone hates ugly problems. You are given a positive integer. You m

Ugly Problem

Ugly Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Special Judge Problem Description Everyone hates ugly problems. You are given a positive integer. You must represent that number by sum of palindromic num

hdu-5920 Ugly Problem(贪心)

题目链接: Ugly Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 363    Accepted Submission(s): 134Special Judge Problem Description Everyone hates ugly problems. You are given a positive inte

String Problem hdu 3374 最小表示法加KMP的next数组

String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1492    Accepted Submission(s): 662 Problem Description Give you a string with length N, you can generate N strings by left shifts.

Interesting Housing Problem HDU - 2426 (KM)

Interesting Housing Problem HDU - 2426 题意:n个人,m个房间,安排住宿.要求每个人不能分到不喜欢的房间,且使满意度最大. 不用slack几乎要超时~ 1 #include <bits/stdc++.h> 2 using namespace std; 3 const int inf=0x3f3f3f3f; 4 const int maxn=550; 5 6 int c[maxn][maxn]; 7 int vb[maxn],vg[maxn],eb[maxn

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

http://acm.hdu.edu.cn/showproblem.php?pid=5920 http://www.cnblogs.com/xudong-bupt/p/4015226.html 把前半部分复制过去,如果太大,那么早到第一个会使得其太大的点,减1,然后对应的中间的变成9 #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <a

【高精度 JAVA】HDU 5920 Ugly Problem

题意 给你一个数字n(n < 10^1000),将其拆成若干个回文串(不超过50个)输出拆分方案 分析 不难想到,我们可以每次给n减一个小于他的最大的回文串,这样能够尽量构造出最少数量的回文串,方法可以使直接将前一半反转贴到后一半,如果比原来的数字大,那么前一半减少1,再反转贴为后一半 比较坑的地方就是 如果构造出来的是11比n大, 那么前一半-1变成了00 ,特判一下,应该为9:如果构造出来是101比n大,应该特判为99 这个题没太多好讲的,我想在这里记录一下java编程心得,以后也会更新 主

(线段树 区间查询)The Water Problem -- hdu -- 5443 (2015 ACM/ICPC Asia Regional Changchun Online)

链接: http://acm.hdu.edu.cn/showproblem.php?pid=5443 The Water Problem Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 738    Accepted Submission(s): 591 Problem Description In Land waterless, w

hdu 5920 Wool 思路

Wool Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Problem Description At dawn, Venus sets a second task for Psyche. She is to cross a river and fetch golden wool from violent sheep who graze on the other side.