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[],sub[];
char ans[][];
char one[]="";
bool judge(char *s){//判断当前数字是否为回文数字
int lens = strlen(s);
for(int i = ;i<lens/;i++){
if(s[i]!=s[lens - i - ]){
return false;
}
}
return true;
}
void decrease(char *s1,char *s2)
{
int len1=strlen(s1);
int len2=strlen(s2);
int i=len1-,j=len2-,flag=;
while(i>=&&j>=) {
if(s1[i]-''-flag>=) {
s1[i]-=flag;
flag=;
}
else {
s1[i] = s1[i] + - flag;
flag = ;
}
if(s1[i]>=s2[j]) {
s1[i]=s1[i]-s2[j]+'';
}
else {
s1[i]=s1[i]+-s2[j]+'';
flag=;
}
i--,j--;
}
while(i>=&&flag) {
if(s1[i]-''>=flag) {
s1[i]-=flag;
flag=;
}
else {
s1[i]=s1[i]+-flag;
flag=;
}
i--;
}
int id=;
bool pre_0=true;
char tmp[];
memset(tmp,,sizeof(tmp));
for(int k=;k<len1;k++) {
if(s1[k]==''&&pre_0) continue;
if(s1[k]!=''&&pre_0) pre_0=false;
tmp[id++]=s1[k];
}
if(!id) {
tmp[id++]='';
}
tmp[id]='\0';
strcpy(s1,tmp);
}
void palindromic(char *s1,char *s2)
{
int len1=strlen(s1),len2;
if(len1==&&s1[]==''&&s1[]==''){
s2[]='';
s2[]='\0';
return;
}
if(len1&) len2=len1/+;
else len2=len1/;
for(int i=;i<len2;i++) {
s2[i]=s1[i];
}
s2[len2]='\0';
decrease(s2,one);
if(s2[]=='') {
s2[]='';
}
for(int i=len1-,j=;j<i;j++,i--) {
s2[i]=s2[j];
}
s2[len1]='\0';
}
int main()
{
//freopen("input.txt","r",stdin);
int T,kase=;
scanf("%d",&T);
while(T--) {
scanf("%s",num);
int id=;
while(num[]!=''&&id<) {
if(judge(num)) {
strcpy(ans[id++],num);
break;
}
memset(sub,,sizeof(sub));
palindromic(num,sub);
strcpy(ans[id++],sub);
decrease(num,sub);
}
printf("Case #%d:\n%d\n",kase++,id);
for(int i=;i<id;i++) {
printf("%s\n",ans[i]);
}
}
}

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

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

    Ugly Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  2. hdu-5920 Ugly Problem(贪心+高精度)

    题目链接: Ugly Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  3. Ugly Problem

    Ugly Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Spec ...

  4. hdu 5920(模拟)

    Ugly Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  5. Flow Problem HDU - 3549

    Flow Problem HDU - 3549 Network flow is a well-known difficult problem for ACMers. Given a graph, yo ...

  6. Prime Ring Problem HDU - 1016 (dfs)

    Prime Ring Problem HDU - 1016 A ring is compose of n circles as shown in diagram. Put natural number ...

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

    http://acm.hdu.edu.cn/showproblem.php?pid=5920 http://www.cnblogs.com/xudong-bupt/p/4015226.html 把前半 ...

  8. HDU 5920 Ugly Problem

    说起这道题, 真是一把辛酸泪. 题意 将一个正整数 \(n(\le 10^{1000})\) 分解成不超过50个回文数的和. 做法 构造. 队友UHC提出的一种构造方法, 写起来比较方便一些, 而且比 ...

  9. HDU 5920 Ugly Problem 高精度减法大模拟 ---2016CCPC长春区域现场赛

    题目链接 题意:给定一个很大的数,把他们分为数个回文数的和,分的个数不超过50个,输出个数并输出每个数,special judge. 题解:现场赛的时候很快想出来了思路,把这个数从中间分为两部分,当位 ...

随机推荐

  1. 编辑器之神vim的一些常用快捷键整理

    yy:复制 光标所在的这一行 4yy:复制 光标所在行开始向下的4行 p:粘贴 dd:剪切(删除) 光标所在的这一行 4dd:剪切(删除) 光标所在行向下的4行 D:从当前的光标开始向后剪切,一直到行 ...

  2. java之类的构造方法

    构造器的特征: 具有和类相同的名称: 不声明返回值的类型: 不能被static.final.synchronized.abstract.native修饰,不能有return语句返回值: 构造器的作用: ...

  3. Springcloud 配置 | 史上最全,一文全懂

    Springcloud 高并发 配置 (一文全懂) 疯狂创客圈 Java 高并发[ 亿级流量聊天室实战]实战系列之15 [博客园总入口 ] 前言 疯狂创客圈(笔者尼恩创建的高并发研习社群)Spring ...

  4. leetcode-math

    Reverse Integer /** * Given a 32-bit signed integer, reverse digits of an integer. * */public class  ...

  5. java自学-数组

    1.数组是什么 前边说过java的基本数据类型,数组,就是装这些基本类型的容器.每个基本类型的变量都是单个的,数组就是这些单个元素的组合. 2.创建数组 方式一 格式: 数组存储的数据类型[] 数组名 ...

  6. java8-新的日期API

    背景 java的日期和时间API设计不理想,java8引入新的时间和日期API就是为了解决这个问题. 老的日期API的核心类 缺点 Date 月从0开始,年最小从1900年开始,没有时区的概念 Cal ...

  7. 挑战10个最难回答的Java面试题(附答案)

    译者:Yujiaao segmentfault.com/a/1190000019962661 推荐阅读(点击即可跳转阅读) 1. SpringBoot内容聚合 2. 面试题内容聚合 3. 设计模式内容 ...

  8. session购物车中的移除功能部分(学生笔记)

    function onclick_remove(r) { if (confirm("确认删除么!此操作不可恢复")) { var out_momey = $(".out_ ...

  9. javaWeb核心技术第七篇之HTTP、Tomcat、Servlet、Request和Response

    - Web服务器 - 概念: - web资源: "英文直译"网"的意思 资源:一切数据文件 web资源:通过网络可以访问到的资源,通常指的是一切放在服务器上的文件&quo ...

  10. arcgis api 4.x for js 集成 Echarts4 实现模拟迁徙图效果(附源码下载)

    前言 关于本篇功能实现用到的 api 涉及类看不懂的,请参照 esri 官网的 arcgis api 4.x for js:esri 官网 api,里面详细的介绍 arcgis api 4.x 各个类 ...