HDU 5920 Ugly Problem 贪心 + java大数+字符串处理

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Tawn0000/article/details/82861344

                                Ugly Problem

                   Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
                                Total Submission(s): 2531    Accepted Submission(s): 834
                                                                             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 (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. 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

题意 : 将一个长度为1000以内的大数分解为50个以内的回文数的和

弹性,每次将当前剩余和贪心长度的一半,然后做成不大于当前剩余和的回文串,然后减去,然后继续更新。

注意特判10,100,110,120,。。。190等情况,且0不能算为回文数

例如

n = 1000000

那么先取100 , 反转成001 和 000 比,如果比000大,就减一 变成了99

做成回文串 99 0 99 ,1000000减去  99099 得 900901

取 900 反转成 009比 901 小, 所以取900,做成回文串 900009,减去 得 892

取 8 反转成8比2大,所以减一变成7,所以 做成回文串797,减去得 95

取9 反转成9比5大,所以减一变成8,所以做成回文串88,减去得7

7已经是回文串了,直接放入

所以 1000000 = 99099 + 900009 + 797 + 88 + 7

如果最后剩下10,100,110的话需要特判一下,不然会死循环,试着理解一下。


import java.util.*;

import java.math.*;

public class Main
{
	public static void main(String args[])
	{
		int T;
		Scanner cin = new Scanner(System.in);
		T = cin.nextInt();
		cin.nextLine();
		for(int t = 1; t <= T; t++)
		{
		String str;
		String str1 ,str2, str3;
		//str = ""+t;
		str = cin.nextLine();
		BigInteger x,y,z;
		Vector vec = new Vector();
		int len = str.length();
		while(len > 1)
		{
			if(str.compareTo("10") == 0) {vec.addElement(9);str = "1";len = 1;continue;}
			if(len == 3 && str.charAt(0) == '1' && str.charAt(2) == '0')
			{
				vec.addElement("99");
				str = new BigInteger(str).subtract(new BigInteger("99")).toString();
				len = str.length();
				continue;
			}
		        x = new BigInteger(str);
			str1 = str.substring(0, len/2);
			str2 = str.substring(len-len/2,len);
//			System.out.println(str);
//			System.out.println(str1);
//			System.out.println(str2);
			str1 = (String)new StringBuffer(str1).reverse().toString();

			y = new BigInteger(str.substring(0, len/2));
			if(str1.compareTo(str2) > 0)  {y =    y.subtract(BigInteger.ONE);}
                        str3 =  y.toString();
			if(len % 2 == 1) {
				str3 += str.charAt(len/2);
			}
			str3 += new StringBuffer(y.toString()).reverse().toString();
			vec.addElement(str3);
			str = x.subtract(new BigInteger(str3)).toString();
			len = str.length();
		}
		if(len == 1 && str.charAt(0) != '0') vec.addElement(str);
                System.out.println("Case #"+ t +":");
		System.out.println(vec.size());
		for(int i = 0; i < vec.size(); i++)
			System.out.println(vec.elementAt(i));
	}
		cin.close();
	}
}
	

猜你喜欢

转载自blog.csdn.net/Tawn0000/article/details/82861344