HDOJ 5920 Ugly Problem(构造+大数相减)
来源:互联网 发布:bit.edu.cn域名 编辑:程序博客网 时间:2024/06/03 16:45
Ugly Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 812 Accepted Submission(s): 286
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.
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 ).
For each test case, there is only one line describing the given integer s (
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
2181000000000000
Sample Output
Case #1:299Case #2:29999999999991Hint9 + 9 = 18999999999999 + 1 = 1000000000000
题意:将一个数s (1<=s<=10^1000) 拆成不超过50个回文数, 这些回文数相加等于s。 要求输出这样的数的个数,并从小到大输出这些数。
题解:每次都构造当前<=s的近似最大的回文数build ,然后 s=s-build 。 用字符串表示的时候注意写法就行了。
代码如下:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define maxn 1010struct Node{char num[maxn];int len;}ans[102];//ans数组开在main函数厉害爆栈了 Node detail(Node str)//构造一个较大的回文数 {int temp=str.len/2+1;str.num[temp]--;for(int i=temp;i<=str.len;++i)if(str.num[i]<'0'){str.num[i+1]--;str.num[i]+=10;}if(str.num[str.len]=='0')str.len--;for(int i=1;i<=str.len/2;++i)str.num[i]=str.num[str.len-i+1];return str;}Node sub(Node res,Node subtor)//大数相减 {for(int i=1;i<=subtor.len;++i){if(res.num[i]>=subtor.num[i])res.num[i]=res.num[i]-subtor.num[i]+'0';else{res.num[i+1]--;res.num[i]+=10;res.num[i]=res.num[i]-subtor.num[i]+'0';}}for(int i=res.len;i>0;--i){if(res.num[i]=='0')res.len--;elsebreak;}return res;}int main(){int t,k=1;scanf("%d",&t);while(t--){Node str;memset(ans,0,sizeof(ans));scanf("%s",str.num+1);str.len=strlen(str.num+1);for(int i=1;i<=str.len/2;++i)//反转字符串,让高位表示高数位 swap(str.num[i],str.num[str.len-i+1]);int cnt=0;while(str.len>1){if(str.len==2&&((str.num[1]-'0'+(str.num[2]-'0')*10)<20)){ans[cnt].num[1]='9';ans[cnt++].len++;str=sub(str,ans[cnt-1]);continue;}Node build=detail(str);ans[cnt++]=build;str=sub(str,ans[cnt-1]);}ans[cnt++]=str;printf("Case #%d:\n%d\n",k++,cnt);for(int i=0;i<cnt;++i){for(int j=ans[i].len;j>=1;--j)putchar(ans[i].num[j]);printf("\n");}}return 0;}
0 0
- HDOJ 5920 Ugly Problem(构造+大数相减)
- HDU 5920 Ugly Problem (大数模拟 构造回文串)
- HDOJ 5920 Ugly Problem 【模拟】
- HDU 5920 Ugly Problem(大数减法 模拟)
- 大数--hdu5920 Ugly Problem
- hdu 5920 Ugly Problem(构造题)
- hdu5920 Ugly Problem(构造)
- hdu5920 Ugly Problem(构造)
- hdu 5920 Ugly Problem 贪心构造 大整数
- hdu 5920 Ugly Problem(CCPC长春,构造回文数)
- hdu 4920 Ugly Problem [模拟+大数减法]
- Hdu 5920 Ugly Problem
- HDU 5920Ugly Problem
- HDOJ 1002 A + B Problem II(大数)
- hdoj 1002 A + B Problem II 大数
- HDOJ/HDU 5686 Problem B(斐波拉契+大数~)
- HDOJ 1002 A + B Problem II(大数)
- Ugly Problem
- Android TabHost + RadioButton实现Tab导航栏
- iOS开发 AFN配置https请求
- CodeVS1085 数字游戏
- CentOS6设置静态IP而且还可以上网
- 输出奖金总数
- HDOJ 5920 Ugly Problem(构造+大数相减)
- ActiveMQ 无法启动 提示端口被占用 解决方案
- html之手打各种符号,如:★ ● 等等
- POI3.8 导出大数据excel
- 初识分布式系统
- 数据结构复习(Updating)
- 150.Best Time to Buy and Sell Stock II-买卖股票的最佳时机 II(中等题)
- linux系统缓存机制
- Android中如何搭建一个WebServer