D - Ugly Problem HDU - 5920
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.
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的更多相关文章
- HDU 5920 Ugly Problem 【模拟】 (2016中国大学生程序设计竞赛(长春))
Ugly Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- hdu-5920 Ugly Problem(贪心+高精度)
题目链接: Ugly Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- Ugly Problem
Ugly Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Spec ...
- hdu 5920(模拟)
Ugly Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- Flow Problem HDU - 3549
Flow Problem HDU - 3549 Network flow is a well-known difficult problem for ACMers. Given a graph, yo ...
- 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 ...
- HDU - 5920 Ugly Problem 求解第一个小于n的回文数
http://acm.hdu.edu.cn/showproblem.php?pid=5920 http://www.cnblogs.com/xudong-bupt/p/4015226.html 把前半 ...
- HDU 5920 Ugly Problem
说起这道题, 真是一把辛酸泪. 题意 将一个正整数 \(n(\le 10^{1000})\) 分解成不超过50个回文数的和. 做法 构造. 队友UHC提出的一种构造方法, 写起来比较方便一些, 而且比 ...
- HDU 5920 Ugly Problem 高精度减法大模拟 ---2016CCPC长春区域现场赛
题目链接 题意:给定一个很大的数,把他们分为数个回文数的和,分的个数不超过50个,输出个数并输出每个数,special judge. 题解:现场赛的时候很快想出来了思路,把这个数从中间分为两部分,当位 ...
随机推荐
- JVM-7-类加载机制
什么是类的加载 类的加载指的是将类的.class文件中的二进制数据读入到内存中,将其放在运行时数据区的方法区内,然后在堆区创建一个java.lang.Class对象,用来封装类在方法区内的数据结构. ...
- 11-《Node.js开发指南》-模块和包
什么是模块? 一个node.js文件就是一个模块,这个文件可能是js代码,json或者编译过的C/C++扩展 创建及加载模块 //a.js var name; exports.setName = fu ...
- Codeforces Round #607 (Div. 1)
A. Cut and Paste 题解 在计算答案的时候,我们发现只需要知道这个字符串前\(l\) 个具体是啥就行了.所以对于每一组询问,我们暴力把这个字符串前\(l\) 的位都算出来,然后剩下的就推 ...
- 洛谷 P4396 (离散化+莫队+树状数组)
### 洛谷P4396 题目链接 ### 题目大意: 有 n 个整数组成的数组,m 次询问,每次询问中有四个参数 l ,r,a,b .问你在[l,r] 的区间内的所有数中,值属于[a,b] 的数的个 ...
- 软件设计之基于Java的连连看小游戏(一)——开题及游戏首页的制作
原本计划紧张忙碌的考试月在图书馆和实验室度过,结果突如其来为期两周的软件设计把课余时间几乎捆绑在了机房.软设没有太多知识上的要求,只要成品简洁美观.实用准确即可.考虑了很久决定要用Java swing ...
- 使用NodeJS模块-第三方提供的模块(什么是npm)
第三方开发者提供的模块 第三方模块是由NodeJS社区或第三方个人开发的功能模块,这些功能模块以软件包的形式存在.被发布在npmjs注册表中.npmjs是一个注册中心,所有软件包的信息都会被记录到该注 ...
- Dynamics CRM邮件附件,你真的了解吗?
关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复160或者20151014可方便获取本文,同时可以在第一时间得到我发布的最新的博文信息,follow me! 听人问起怎么读取到一封邮件所有的 ...
- ABP入门教程14 - 更新多语言
点这里进入ABP入门教程目录 设置语种 新增语种 数据库操作 打开多语言表AbpLanguages,添加一条记录. 程序操作 在基础设施层(即JD.CRS.EntityFrameworkCore)的\ ...
- python小技巧 小知识
python小技巧 小知识 python系统变量(修改调用shell命令路径)或用户空间说明 20150418 python调用系统命令,报找不到.怎么办? 类似执行shell的: [ -f /etc ...
- Android Studio如何配置CURL指令一键打包apk上传至蒲公英
Android Studio如何配置CURL指令一键打包apk上传至蒲公英 第一步:在所需要打包的模块build.gradle文件中加入如下代码: android{ buildTypes { //配置 ...