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 (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
 
题意:找到不超过 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 = ;
while(tcase-->){
String str = sc.next();
String [] ans = new String[];
System.out.println("Case #"+(t++)+":");
int n = ;
if(ispalindromic(str)) {
System.out.println();
System.out.println(str);
continue;
}
while(true){
String str1=str;
if(str.length()==||ispalindromic(str)){
ans[n++] = str;
break;
}
if(str.length()%==){
int len = str.length()/;
if(len==&&str.charAt()==''){
if(str.compareTo("")>=)
str1 = "";
else str1 = "";
}else{
String temp ="";
for(int i=;i<len;i++){
temp+=str.charAt(i);
}
temp = new BigInteger(temp).subtract(BigInteger.ONE).toString();
len = temp.length();
for(int i=len-;i>=;i--){
temp+= temp.charAt(i);
}
str1 = temp;
}
}else{
int len = str.length()/;
if(len==&&str.charAt()==''){
BigInteger b = new BigInteger(str);
for(int i=b.intValue();i>=;i--){
b = b.subtract(BigInteger.ONE);
if(ispalindromic(b.toString())){
str1 = b.toString();
break;
}
}
}else{
String temp ="";
for(int i=;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-;i>=;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-);
for(int i=;i<n;i++){
System.out.println(ans[i]);
}
}
} static boolean ispalindromic(String s){
int len = s.length();
for(int i=,j=len-;i<=j;i++,j--){
if(s.charAt(i)!=s.charAt(j)) return false;
}
return true;
}
}
 

hdu 5920(模拟)的更多相关文章

  1. D - Ugly Problem HDU - 5920

    D - Ugly Problem HDU - 5920 Everyone hates ugly problems. You are given a positive integer. You must ...

  2. HDU 5920 Ugly Problem 【模拟】 (2016中国大学生程序设计竞赛(长春))

    Ugly Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  3. HDU 5920 Ugly Problem 高精度减法大模拟 ---2016CCPC长春区域现场赛

    题目链接 题意:给定一个很大的数,把他们分为数个回文数的和,分的个数不超过50个,输出个数并输出每个数,special judge. 题解:现场赛的时候很快想出来了思路,把这个数从中间分为两部分,当位 ...

  4. hdu 4891 模拟水题

    http://acm.hdu.edu.cn/showproblem.php?pid=4891 给出一个文本,问说有多少种理解方式. 1. $$中间的,(s1+1) * (s2+1) * ...*(sn ...

  5. hdu 5012 模拟+bfs

    http://acm.hdu.edu.cn/showproblem.php?pid=5012 模拟出骰子四种反转方式,bfs,最多不会走超过6步 #include <cstdio> #in ...

  6. HDU - 5920 Ugly Problem 求解第一个小于n的回文数

    http://acm.hdu.edu.cn/showproblem.php?pid=5920 http://www.cnblogs.com/xudong-bupt/p/4015226.html 把前半 ...

  7. hdu 4669 模拟

    思路: 主要就是模拟这些操作,用链表果断超时.改用堆栈模拟就过了 #include<map> #include<set> #include<stack> #incl ...

  8. 2013杭州网络赛C题HDU 4640(模拟)

    The Donkey of Gui Zhou Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  9. HDU/5499/模拟

    题目链接 模拟题,直接看代码. £:分数的计算方法,要用double; #include <set> #include <map> #include <cmath> ...

随机推荐

  1. Middle of Linked List

    Find the middle node of a linked list. Example Given 1->2->3, return the node with value 2. Gi ...

  2. Hive:HQL和Mysql:SQL 的区别

    HQL: group by 后面的参数一定要和select非聚集函数一致 where 1 要改成 where 1 = 1

  3. 【CF123E】Maze

    Portal --> cf123E Solution 首先步数的话可以转化成每条边经过了几次这样来算 假设现在确定了起点\(S\)和终点\(T\),我们将\(T\)看成树根,那么考虑边\((u, ...

  4. 如何设置eclipse格式化xml代码时不自动换行

    如何设置eclipse格式化代码时不自动换行 2015年12月23日 09:08:36 qq_20889581 阅读数:3770 标签: eclipse格式化android 更多 个人分类: Ecli ...

  5. input 拍照上传

    <input id="up2" type="file" accept="image/*" capture="camera&q ...

  6. IO多路复用之epoll(一)讲解

    网络通信中socket有自己的内核发送缓冲区和内核接受缓冲区,好比是一个水池, 当用户发送数据的时候会从用户缓冲区拷贝到socket的内核发送缓冲区,然后从 socket发送缓冲区发出去, 当用户要读 ...

  7. 使用nginx+docker配置https负载均衡

    了解Docker Docker是一个golang编写的开源轻量级的.可移植的.自给自足的容器,Docker主要应用在以下场景: web应用的自动化打包和发布: 自动化测试和持续集成.发布: 在服务型环 ...

  8. Tensorflow BatchNormalization详解:1_原理及细节

    Batch Normalization: 原理及细节 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 吴恩达deeplearningai课程 课程笔记 Udacity课程 为了标准化 ...

  9. CSS盒子知识

    此随笔写于学习完CSS盒子之后,所遇到的问题和感悟记录. 1.IE盒子: IE盒子的特性:对于IE浏览器来说width不是内容宽度.而是内容+外边距+边框的内容总和. 也就是说当盒子增加10px;那么 ...

  10. 分治法:三维偏序问题之CDQ分治

    我怀疑那个k是用来定界限用的 #include <cstdio> #include <cstring> #include <algorithm> using nam ...