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> ...
随机推荐
- 自动化测试常用断言的使用方法(python)
自动化测试中寻找元素并进行操作,如果在元素好找的情况下,相信大家都可以较熟练地编写用例脚本了,但光进行操作可能还不够,有时候也需要对预期结果进行判断. 这里介绍几个常用断言的使用方法,可以一定程度上帮 ...
- 百万级运维心得一:Mongodb和Redis数据不能放在同一个服务器
百万级运维经验一:Mongodb和Redis数据不能放在同一个服务器 一开始时,为了省服务器,把Mongodb和Redis放在一个服务器上.网站每到高峰期都特别卡,还经常出现502.找了很久的原因,发 ...
- 【bzoj1022】小约翰的游戏John
Portal -->bzoj1022 Solution 这题其实是裸的反Nim,这里主要是为了写反Nim游戏的证明 首先给出反Nim(anti-nim)的定义和结论: [定义]桌子上 ...
- git使用经验(一)
在使用Git Push代码到数据仓库时,提示如下错误: [remote rejected] master -> master (branch is currently checked out) ...
- 《剑指offer》— JavaScript(11)二进制中1的个数
二进制中1的个数 题目描述 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 思路一 用1和n进行位运算,结果为1则n的二进制最右边一位为1,否则为0: 将n二进制形式右移1位,继续与 ...
- selenium测试 - open Firefox
环境:Python2.7+selenium3+Firefox47 问题1: 在打开火狐浏览器时报错:‘geckodriver‘ executable needs to be in PATH fro ...
- laravel5.1 关联模型保存的方法(使用associate方法)
模型定义 class User { public function customer() { return $this->hasOne('Customer'); } } class Custom ...
- springMVC和mybatis的原理
mybatis是什么? mybatis是一个持久层框架,是apache下的开源项目,前身是itbatis,是一个不完全的ORM框架,mybatis提供输入和输出的映射,需要程序员自己写sql语句,my ...
- C++时间
C++时间 头文件 chrono, 命名空间 std. 现在时间 std::chrono::system_clock::now() 返回系统时钟的当前时间 时钟 std::chrono::system ...
- 基于JavaSE阶段的IO流详解
1.IO流基本概述 在Java语言中定义了许多针对不同的传输方式,最基本的就是输入输出流(俗称IO流),IO流是属于java.io包下的内容,在JavaSE阶段主要学下图所示的: 其中从图中可知,所有 ...