Ugly Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 515 Accepted Submission(s): 206
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.
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.
Input
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≤10
1000![]()
).
For each test case, there is only one line describing the given integer s ( 1≤s≤10
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. 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 1Hint9 + 9 = 18 999999999999 + 1 = 1000000000000
Source
题意:给一个数,要求构造为几个回文数相加的形式。
思路:因为给出的数字很大所以肯定要用到高精度。因为觉得C++的高精度实现太复杂,所以采用了JAVA的BigInteger的类实现,结果实现过程中写的各种精神分裂,因为不会用JAVA啊(摔)。最后听从zy1的更好的方法,直接把字符串对半分,判断奇偶判断相减大小然后分别处理,但是因为思维不清晰导致在处理诸如100000这样的数字时还是各种麻烦。今天早上控制住几次暴走的心情写了一个,没想到交上去瞬间1A哈哈哈哈哈哈,我果然是个天才。
import java.math.BigInteger;
import java.util.Scanner;
public class Main{
public static void main(String args[]){
Scanner cin = new Scanner(System.in);
int n=cin.nextInt();
String temp=cin.nextLine();
for(int cas=1;cas<=n;cas++){
String ans[]=new String[55];//存放答案
String s=cin.nextLine();//一开始的数
System.out.println("Case #"+cas+":");
int cnt=0;
while(s!="0"){
int k=s.length();
String s1=s.substring(0,k/2);//字符串对半
StringBuffer s2 = new StringBuffer(s1);//字符串变成StringBuffer
s2.reverse();
StringBuffer s3=new StringBuffer(s1);
if(k%2!=0){
s3=s3.append(s.substring(k/2, k/2+1));
s3=s3.append(s2);
}
else{
s3=s3.append(s2);
}
//System.out.println(s3.toString());
BigInteger a=new BigInteger(s3.toString());//构造的回文数
BigInteger b = new BigInteger(s);//原来的数
if(b.compareTo(a)==0||b.compareTo(a)==1){
ans[cnt]=a.toString();
cnt++;
s=b.subtract(a).toString();
//System.out.println(s);
}
if(b.compareTo(a)==-1){
int l=k/2-1;
char c=s3.charAt(l);
while(c=='0'){;
l--;
c=s3.charAt(l);
}
String s4=new String("");
if(l!=0){
s4=s3.toString().substring(0, l);//保留前面的数字
s3=new StringBuffer(s4);
s2=new StringBuffer(s4);
s3.append((char)(c-1));//中间旁边数字-1
s2.append((char)(c-1));
if(k%2!=0){
s2.append("9");
}
s3.reverse();
s2=s2.append(s3);
ans[cnt]=s2.toString();
cnt++;
a=new BigInteger(s2.toString());//构造的回文数
b = new BigInteger(s);//原来的数
s=b.subtract(a).toString();
}
else{
s3=new StringBuffer(s4);
if((char)(c-1)!='0'){
s3.append((char)(c-1));
}
for(int i=0;i<k-2;i++)
s3.append("9");
if((char)(c-1)=='0')
s3.append("9");
else
s3.append((char)(c-1));
ans[cnt]=s3.toString();
cnt++;
a=new BigInteger(s3.toString());//构造的回文数
b = new BigInteger(s);//原来的数
s=b.subtract(a).toString();
}
}
}
System.out.println(cnt);
for(int i=0;i<cnt;i++)
System.out.println(ans[i]);
}
}
}