HDU 5920 Ugly Problem 高精度减法大模拟 ---2016CCPC长春区域现场赛
题意:给定一个很大的数,把他们分为数个回文数的和,分的个数不超过50个,输出个数并输出每个数,special judge。
题解:现场赛的时候很快想出来了思路,把这个数从中间分为两部分,当位数为偶数的时候3456就分为34和56,34-1=33,回文数3333,3456-3333=123然后继续算;当位数为奇数的时候34567就分为34和67,5-1=4,回文数34443,34567-34443=124然后继续算。但是一年都没有写过高精度减法的题了,这个大模拟写了很久最后判断奇偶性都判断蒙了- - !,汗。考虑的太复杂了,前导0,奇偶性,如果前面比后面小就不减1的情况,有的是我想多了,找了一份比较短的代码虚心学习一下,写的很棒。
#include<bits/stdc++.h>
using namespace std;
const int N=+; bool getPal(char *s) { //得到回文数
int n=strlen(s);
if(n==)return ;
if(n==&&s[]=='') {
s[]='';
s[]=;
return ;
}
s[(n+)/-]--;
for(int i=(n+)/-; i>; i--) {
if(s[i]<'')s[i]+=,s[i-]--;
else break;
}
if(s[]=='') {
for(int i=; i<n; i++)s[i]=s[i+];
n--;
}
for(int i=(n+)/; i<n; i++)
s[i]=s[n-i-];
return ;
}
bool subStr(char *a,char *b) { //减去回文数
int na=strlen(a);
int nb=strlen(b);
for(int i=na-,j=nb-; j>=; i--,j--) {
if(a[i]>=b[j])a[i]=a[i]-b[j]+'';
else a[i]=a[i]+-b[j]+'',a[i-]--;
}
int p;
for(p=; a[p]==''; p++);
if(p==na)return ;
for(int i=; p<=na; p++,i++)a[i]=a[p];
return ;
}
char s[N],ans[][N];
int cnt;
int main() {
//freopen("f.txt","r",stdin);
int T;
scanf("%d",&T);
for(int cas=; cas<=T; cas++) {
scanf("%s",s);
for(cnt=;; cnt++) {
strcpy(ans[cnt],s);
if(getPal(ans[cnt])||subStr(s,ans[cnt]))break;
} printf("Case #%d:\n%d\n",cas,cnt+);
for(int i=; i<=cnt; i++)
printf("%s\n",ans[i]);
}
return ;
}
HDU 5920 Ugly Problem 高精度减法大模拟 ---2016CCPC长春区域现场赛的更多相关文章
- 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 求解第一个小于n的回文数
http://acm.hdu.edu.cn/showproblem.php?pid=5920 http://www.cnblogs.com/xudong-bupt/p/4015226.html 把前半 ...
- HDU 5920 Ugly Problem
说起这道题, 真是一把辛酸泪. 题意 将一个正整数 \(n(\le 10^{1000})\) 分解成不超过50个回文数的和. 做法 构造. 队友UHC提出的一种构造方法, 写起来比较方便一些, 而且比 ...
- HDU 1022 Train Problem I(栈模拟)
传送门 Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of st ...
- hdu 4771 Stealing Harry Potter's Precious (2013亚洲区杭州现场赛)(搜索 bfs + dfs) 带权值的路径
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4771 题目意思:'@' 表示的是起点,'#' 表示的是障碍物不能通过,'.' 表示的是路能通过的: ...
- HDU 4811 Ball -2013 ICPC南京区域现场赛
题目链接 题意:三种颜色的球,现给定三种球的数目,每次取其中一个放到桌子上,排成一条线,每次放的位置任意,问得到的最大得分. 把一个球放在末尾得到的分数是它以前球的颜色种数 把一个球放在中间得到的分数 ...
- 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(贪心+高精度)
题目链接: Ugly Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- hdu 5920(模拟)
Ugly Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
随机推荐
- CF #305(Div.2) D. Mike and Feet(数学推导)
D. Mike and Feet time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Linux文件处理命令
1.权限处理 1.1 方法一 使用+-=的方法 1.1.1权限 rwx r 读 w 写 x 执行 1.1.2用户 ugoa u 所有者 g 用户组 o 其他人 a 表示以上所有 修改文件的方法 例: ...
- 汉字转拼音(pinyin4j)
1.引入依赖 <dependency> <groupId>pinyin4j.sourceforge.net</groupId> <artifactId> ...
- javascript高级程序设计---NodeList和HTMLCollection
节点对象都是单个节点,但是有时会需要一种数据结构,能够容纳多个节点.DOM提供两种接口,用于部署这种节点的集合分别是NodeList和HTMLCollection MDN上的定义: NodeList: ...
- HashMap原理分析
HashMap 实现Map.Cloneable.Serializable接口,继承AbstractMap基类. HashMap map = new HashMap(); 实例化一个HashMap,在构 ...
- ubuntu 15.04 手动安装nginx 1.9.0
平时工作也用nginx,不过用的时候都是已经配好的,只要简单改改参数就可以了.今天在自己的电脑上安装的时候发现没有想象的那么顺利. 纸上得来终觉浅,绝知此事要躬行. 正题: 1.到nginx下载页面获 ...
- BZOJ4411——[Usaco2016 Feb]Load balancing
1.题意: 给出N个平面上的点.保证每一个点的坐标都是正奇数. 你要在平面上画两条线,一条是x=a,一条是y=b,且a和b都是偶数. 直线将平面划成4个部分,要求包含点数最多的那个部分点数最少. 2. ...
- 流媒体(音频 AudioStreamer)
AudioStreamer 在github可以搜索到, 专门播放流媒体(音频)
- leetcode 63. Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- c++ 操作符重载和友元
操作符重载(operator overloading)是C++中的一种多态,C++允许用户自定义函数名称相同但参数列表不同的函数,这被称为函数重载或函数多态.操作符重载函数的格式一般为: operat ...