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
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.
For each test case, there is only one line describing the given integer s (1≤s≤101000).
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.
18
1000000000000
2
9
9
Case #2:
2
999999999999
1
题解:这题我是这样想的,开始的想法是每次找到一个最接近 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(模拟)的更多相关文章
- D - Ugly Problem HDU - 5920
D - Ugly Problem HDU - 5920 Everyone hates ugly problems. You are given a positive integer. You must ...
- HDU 5920 Ugly Problem 【模拟】 (2016中国大学生程序设计竞赛(长春))
Ugly Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- HDU 5920 Ugly Problem 高精度减法大模拟 ---2016CCPC长春区域现场赛
题目链接 题意:给定一个很大的数,把他们分为数个回文数的和,分的个数不超过50个,输出个数并输出每个数,special judge. 题解:现场赛的时候很快想出来了思路,把这个数从中间分为两部分,当位 ...
- hdu 4891 模拟水题
http://acm.hdu.edu.cn/showproblem.php?pid=4891 给出一个文本,问说有多少种理解方式. 1. $$中间的,(s1+1) * (s2+1) * ...*(sn ...
- hdu 5012 模拟+bfs
http://acm.hdu.edu.cn/showproblem.php?pid=5012 模拟出骰子四种反转方式,bfs,最多不会走超过6步 #include <cstdio> #in ...
- HDU - 5920 Ugly Problem 求解第一个小于n的回文数
http://acm.hdu.edu.cn/showproblem.php?pid=5920 http://www.cnblogs.com/xudong-bupt/p/4015226.html 把前半 ...
- hdu 4669 模拟
思路: 主要就是模拟这些操作,用链表果断超时.改用堆栈模拟就过了 #include<map> #include<set> #include<stack> #incl ...
- 2013杭州网络赛C题HDU 4640(模拟)
The Donkey of Gui Zhou Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- HDU/5499/模拟
题目链接 模拟题,直接看代码. £:分数的计算方法,要用double; #include <set> #include <map> #include <cmath> ...
随机推荐
- POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)
POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...
- NOIP2017结束了
NOIP 2017 两天T1总分80 D1T3爆零 学军数据255 洛谷数据270 大概连1=都没有吧 没有预料到的结果 想了很久 最后只能选择接受 看完成绩单 高二爷们也许只剩下CYC LLQ ZS ...
- 关于OpenCV的stitching使用
配置环境:VS2010+OpenCV2.4.9 为了使用OpenCV实现图像拼接头痛了好长时间,一直都没时间做,今天下定决心去实现基本的图像拼接. 首先,看一看使用OpenCV进行拼接的方法 基本都是 ...
- Hdu5693 D Game
D Game Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Subm ...
- poj1204 Word Puzzles
Word Puzzles Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 12090 Accepted: 4547 S ...
- 11.UiAutomator 相关JAVA知识
一.封装方法与模块化用例 1.方法: 在JAVA中,方法就好比日常生活中的一个动作,由动作组合成一系列完整的操作. 方法结构: 方法修饰符 方法返回值类型 方法名 { 方法体 } 比如: public ...
- JS-this的用法
o.onclick=function(){alert(this)}//这个this是指o ------ var arr=[1,2,3,4,5]; arr.a=12; arr.show=function ...
- JAVA类与对象---实例变量与类变量的区别,实例方法和类方法的区别
实例变量 实例变量声明在一个类中,但在方法.构造方法和语句块之外: 当一个对象被实例化之后,每个实例变量的值就跟着确定: 实例变量在对象创建的时候创建,在对象被销毁的时候销毁: 实例变量的值应该至少被 ...
- HDU 4946 凸包
给你n个点,具有速度,一个位置如果有其他点能够先到,则不能继续访问,求出里面这些点哪些点是能够无限移动的. 首先我们考虑到,一个速度小的和一个速度大的,速度小的必定只有固定他周围的一定区域是它先到的, ...
- HDU 4778 状压DP
一看就是状压,由于是类似博弈的游戏.游戏里的两人都是绝对聪明,那么先手的选择是能够确定最终局面的. 实际上是枚举最终局面情况,0代表是被Bob拿走的,1为Alice拿走的,当时Alice拿走且满足变换 ...