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.3087 Shuffle'm Up (模拟)
POJ.3087 Shuffle'm Up (模拟) 题意分析 给定两个长度为len的字符串s1和s2, 接着给出一个长度为len*2的字符串s12. 将字符串s1和s2通过一定的变换变成s12,找到 ...
- String和stringbuffer和stringbuilder的区别
String 字符串常量 StringBuffer 字符串变量(线程安全) StringBuilder 字符串变量(非线程安全) 简要的说, String 类型和 StringBuffer 类型的主要 ...
- Android Monkey 脚本编写与检查内存泄露
一.Monkey脚本编写 1.Monkey脚本格式 脚本优势: 简单快捷,不需要接触任何工具,只需要一个记事本文件 脚本缺点: 实现坐标.按键等基本操作的相应步骤,顺序脚本无逻辑性 脚本源码: \de ...
- redis服务启动脚本
/etc/rc.d/init.d/redis #!/bin/sh# chkconfig: 2345 80 90 # description: Start and Stop redis REDISPOR ...
- CSS3方法总汇
PS:CSS3的3D和我做研发时的3D不一样他们只能旋转180度 横为X竖为Z高为Y transfrom:2D3D转换 rotareX:绕着X轴旋转 rotareY(-180deg):绕着Y轴旋转- ...
- LightOJ 1065 - Number Sequence 矩阵快速幂水题
http://www.lightoj.com/volume_showproblem.php?problem=1065 题意:给出递推式f(0) = a, f(1) = b, f(n) = f(n - ...
- ① 设计模式的艺术-01.单例(Singleton)模式
单例模式为何要出现 在工作过程中,发现所有可以使用单例模式的类都有一个共性,那就是这个类没有自己的状态,换句话说,这些类无论你实例化多少个,其实都是一样的. 如果我们不将这个类控制成单例的结构,应用中 ...
- 【BZOJ】1597 [Usaco2008 Mar]土地购买
[算法]DP+斜率优化 [题意]n(n≤50000)块土地,长ai宽bi,可分组购买,每组代价为max(ai)*max(bi),求最小代价. [题解] 斜率优化:http://www.cnblogs. ...
- 【BZOJ做题记录】07.07~?
在NOI一周前重开一个坑 最后更新时间:7.08 07:38 7.06 下午做的几道CQOI题: BZOJ1257: [CQOI2007]余数之和sum:把k mod i写成k-k/i*i然后分段求后 ...
- vue-router.esm.js?fe87:16 [vue-router] Route with name 'page' does not exist
本文地址:http://www.cnblogs.com/veinyin/p/7910525.html 我的路由配置 { path: '/page', name: page, component: pa ...