hdu 5920(模拟)

Ugly Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 190    Accepted Submission(s): 74
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.
 
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 (1s101000).
 
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 1
 
题意:找到不超过 50 个元素的一个回文数组,使得 这些数组之和 为输入的大整数 sum
 
题解:这题我是这样想的,开始的想法是每次找到一个最接近 sum的回文数 ,然后一直去减,但是后来发现这个数不好找,就决定找一个足够大的接近sum的回文数,怎么找呢?我们知道找到比sum大的那个是取 sum的前一半然后进行+1,然后去补齐后一半,照这样的思路我们可以取其前一半-1,然后去补后一半,每次长度可以减半,所以不会超过50次,所以马马虎虎AC..
import java.math.BigInteger;
import java.util.Scanner;


public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int tcase = sc.nextInt();
        int t  = 1;
        while(tcase-->0){
            String str = sc.next();
            String [] ans = new String[1000];
            System.out.println("Case #"+(t++)+":");
            int n = 1;
            if(ispalindromic(str)) {
                System.out.println(1);
                System.out.println(str);
                continue;
            }
            while(true){
                String str1=str;
                if(str.length()==1||ispalindromic(str)){
                    ans[n++] = str;
                    break;
                }
                if(str.length()%2==0){
                    int len = str.length()/2;
                    if(len==1&&str.charAt(0)=='1'){
                        if(str.compareTo("11")>=0)
                        str1 = "11";
                        else str1 = "9";
                    }else{
                        String temp ="";
                        for(int i=0;i<len;i++){
                            temp+=str.charAt(i);
                        }
                        temp = new BigInteger(temp).subtract(BigInteger.ONE).toString();
                        len = temp.length();
                        for(int i=len-1;i>=0;i--){
                            temp+= temp.charAt(i);
                        }
                        str1 = temp;
                    }
                }else{
                    int len = str.length()/2;
                    if(len==1&&str.charAt(0)=='1'){
                        BigInteger b = new BigInteger(str);
                        for(int i=b.intValue();i>=1;i--){
                            b = b.subtract(BigInteger.ONE);
                            if(ispalindromic(b.toString())){
                                str1 = b.toString();
                                break;
                            }
                        }
                    }else{
                        String temp ="";
                        for(int i=0;i<len;i++){
                            temp+=str.charAt(i);
                        }
                        temp = new BigInteger(temp).subtract(BigInteger.ONE).toString();
                        len = temp.length();
                        temp+=str.charAt(len);
                        for(int i=len-1;i>=0;i--){
                            temp+= temp.charAt(i);
                        }
                        str1 = temp;
                    }
                }
                ans[n++] = str1;
                BigInteger big1 = new BigInteger(str);
                BigInteger big2 = new BigInteger(str1);
                BigInteger big = big1.subtract(big2);
                str = big.toString();
            }
            System.out.println(n-1);
            for(int i=1;i<n;i++){
                System.out.println(ans[i]);
            }
        }
    }
    
    static boolean ispalindromic(String s){
        int len = s.length();
        for(int i=0,j=len-1;i<=j;i++,j--){
            if(s.charAt(i)!=s.charAt(j)) return false;
        }
        return true;
    }
}
 
原文地址:https://www.cnblogs.com/liyinggang/p/5931408.html