Ugly Problem
Special Judge
Problem Description
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.
In 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 ).
Output
For 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. Then 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 1Hint9 + 9 = 18 999999999999 + 1 = 1000000000000
懒得写,,,,
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static String temp;
public static int len;
public static boolean Judge(char str[]) {//这个是判断它是否已经是回文串的函数
len = str.length;
for(int i = 0; i < len / 2; i++) {
if(str[i] != str[len - 1 - i]) return false;
}
return true;
}
public static boolean Judge2(char str[]) {//这个是判断是不是类(1000)的数字,这样的话直接减一就可以了(其实没必要,只是我当时写晕了)
if(str[0] != '1') return false;
len = str.length;
for(int i = 1; i < len; i ++) {
if(str[i] != '0') return false;
}
return true;
}
public static BigInteger Next(BigInteger re) {//这个是求不大于re的最大回文数
temp = re.toString();
char[] str = temp.toCharArray();
if(Judge(str)) return re;//如果已经是回文数,直接返回
else if(Judge2(str)) return re.subtract(BigInteger.ONE);//如果是类1000型的,直接返回re-1
else {
/**
*忽略此段
我感觉我写的简直渣渣,,,,,,,,,
否则的话就要一点一点的判断……累,泪,,,从中间向两边判断,
如果已经保证生成的数字串比之前的小了(flag == true)那么新串的两个相对应的位置直接赋值为原串中高位的值
比如: abcdef
abccba
如果已经知道了cd>cc那么abcdef肯定大于abccba
如果没有比之前的小,就要分情况而论,
如果原串高位的值大于等于低位的值,那么
如果这个时候原串中对应的两个位置的两个值都是0,那么这时候当然不能都 直接取0,因为既然前面没有做到新串
<原串,那么必然新串==原串(因为新串不能大于原串的) 注意我说的新串和原串都是当前访问的两个位置之间的串,也就是(i,len - 1 i)的串,因为是从中间向两边扫的。既然之前原串等于新串,那么这个时候如果不处理,那么只能委屈高位 不写了,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ** len char[] the="new" char[len]; int i % 2 0 ? : 2; boolean flag="false;" llll="false;" for(;>
= 0; i --) {
if(flag == false) {
if(str[len - i - 1] == str[i] && str[i] == '0') {
the[i] = '9';
the[len - i - 1] = '9';
llll = true;
} else if(str[i] == '0' && str[len - i - 1] != '0') {
if(llll == true) {
the[i] = '9';
the[len - i - 1] = '9';
}else {
the[i] = '0';
the[len - i - 1] = '0';
flag = true;
}
}else if(str[i] < str[len - i - 1]){
if(llll == true) {
the[i] = (char)(str[i] - 1);;
the[len - i - 1] = (char)(str[i] - 1);
flag = true;
}else {
the[i] = str[i];
the[len - i - 1] = str[i];
flag = true;
}
}else {
the[i] = (char)(str[i] - 1);
the[len - i - 1] = the[i];
flag = true;
}
}else {
the[i] = str[i];
the[len - i - 1] = str[i];
}
}
if(the[0] == '0') the[len - 1] = '9';
temp = String.valueOf(the);
re = new BigInteger(temp);
return re;
}
}
public static void main(String[] args) {
Scanner Cin = new Scanner(System.in);
int t = Cin.nextInt();
BigInteger[] ans = new BigInteger[55];
for(int Case = 1; Case <= t; Case ++) {
int l = 0;
BigInteger n = Cin.nextBigInteger();
System.out.printf("Case #%d:",Case);
System.out.println();
while(n.compareTo(BigInteger.ZERO) != 0) {
ans[l] = Next(n);
n = n.subtract(ans[l]);
l ++;
}
System.out.println(l);
for(int i = 0; i < l; i ++) {
System.out.println(ans[i]);
}
}
}
}