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.
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 1000000000000Sample Output
Case #1: 2 9 9 Case #2: 2 999999999999 1Hint
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]=‘