hdu 5920 Ugly Problem 贪心构造 大整数

Ugly Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 347    Accepted Submission(s): 131
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
Hint
9 + 9 = 18 999999999999 + 1 = 1000000000000
 

Source

题意:将一个大整数分解为少于等于50个的回文数字。special judge

思路:设a为给的数
while(a>=20)
{
x=a取前一半
x--
x+= reverse(x)
a=a-x
}


import java.math.BigInteger;
import java.util.*;
public class Main {
//用java提交的时候需将类名改为Main
    @SuppressWarnings("null")
    public static void main(String[] args) {
        int t;
        Scanner re=new Scanner(System.in);
        t=re.nextInt();
        BigInteger a=BigInteger.valueOf(0),tmpa,tmpa1,ten=BigInteger.valueOf(20);
        String s=null,tmps=null,tmps1=null,tmps2=null;
        String ss[]=new String[55];
        int ind=0;
        for(int cns=1;cns<=t;cns++)
        {
            ind=0;
            a=re.nextBigInteger();
            System.out.printf("Case #%d:\n",cns);
            while(a.compareTo(ten)>=1)
            {
            s=a.toString();
            int len=s.length(); 
            tmps=s.substring(0,(len+1)/2);
            tmpa=new BigInteger(tmps);
            tmpa=tmpa.subtract(BigInteger.valueOf(1));
            tmps1=tmpa.toString();
            if(len%2==0)
            {
                tmps1=tmpa.toString();
                int len1=tmps1.length();
                tmps2="";
                for(int i=len1-1;i>=0;i--)
                {
                    tmps2+=tmps1.charAt(i);
                }
                tmps=tmps1+tmps2;
                tmpa1=new BigInteger(tmps);
                a=a.subtract(tmpa1);
                ss[ind++]=tmps;
                //System.out.println(tmps);
            }
            else
            {
                tmps1=tmpa.toString();
                int len1=tmps1.length();
                tmps2="";
                for(int i=len1-2;i>=0;i--)
                {
                    tmps2+=tmps1.charAt(i);
                }
                tmps=tmps1+tmps2;
                tmpa1=new BigInteger(tmps);
                a=a.subtract(tmpa1);
                ss[ind++]=tmps;
                //System.out.println(tmps);
            }
            }
            long ans=a.longValue();
            if(ans>=10)
            {
                ss[ind++]="9";
                ss[ind++]=String.valueOf(ans-9);
                //System.out.println(9);
                //System.out.println(ans-9);
            }
            else
                ss[ind++]=String.valueOf(ans);
                //System.out.println(ans);
            System.out.println(ind);
            for(int i=0;i<ind;i++)
            {
                System.out.println(ss[i]);
            }
        }
        
    }

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值