hdu 5920 Ugly Problem 字符串处理
来源:互联网 发布:淘宝网申请退货 编辑:程序博客网 时间:2024/06/17 08:14
Ugly Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1336 Accepted Submission(s): 453
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≤10
1000![]()
![]()
).
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
Source
2016中国大学生程序设计竞赛(长春)-重现赛
#include<iostream>#include<algorithm>using namespace std;const int N = 1010;char a[N],b[N],c[N],ans[N][N];int main(){ int T,cas = 1; scanf("%d",&T); while(T--) { scanf("%s",a); int num = 0,len = strlen(a); while(len >= 1) { //获取回文数字b int flag = 0; b[len >> 1] = a[len >> 1]; for(int i = len / 2 - 1; i >= 0; i--) { if(a[i] != a[len - 1 - i] && !flag)//第一次找到不对称的地方 { flag = 1; b[i] = b[len - 1 - i] = min(a[i],a[len - 1 - i]); } else b[i] = b[len - 1 - i] = a[i]; } b[len] = 0; //特判 if(!flag)//a已经是回文串 { strcpy(ans[num++],a); break; } if(b[0] == '0')//特判 { if(a[0] == '1')//分成1和999999..... { b[0] = '1',b[1] = 0; strcpy(ans[num++],b); for(int i = 0; i < len - 1; i++) b[i] = '9'; b[len - 1] = 0; strcpy(ans[num++],b); } else//分成9+199..991,8 + 299..992,7 + 399..993等 { b[0] = b[len - 1] = (char)(a[0] - 1); for(int i = 1; i < len - 1; i++) b[i] = '9'; b[len] = 0; strcpy(ans[num++],b); int t6 = 11 - (int)(a[0] - '0'); b[0] = (char)(t6 + '0'); b[1] = 0; strcpy(ans[num++],b); } if((len & 1) && (a[len >> 1] != '0'))//奇数位中间位置可能不为0,需要再次计算 { strcpy(a,a + len/2); len = (len + 1) >> 1; continue; } break; } // 获取c,c = a - b int t4 = 0; for(int i = len - 1; i >= 0; i--) { int t1 = a[i] - '0', t2 = b[i] - '0'; int t3 = (t1 - t2 - t4 + 10) % 10; c[i] = (char)(t3 + '0'); t4 = (int)((a[i] - b[i] - t4) < 0); } c[len] = 0; //清除c的前导零 int t5; for(int i = 0; i < len; i++) { if(c[i] != '0') { t5 = i; break; } } strcpy(ans[num++],b); strcpy(a,c + t5);//将c赋值给a继续下一次循环 len = strlen(a); } printf("Case #%d:\n",cas++); printf("%d\n",num); for(int i = 0; i < num; i++) printf("%s\n",ans[i]); } return 0;}
阅读全文
0 0
- hdu 5920 Ugly Problem 字符串处理
- Hdu 5920 Ugly Problem
- HDU 5920Ugly Problem
- HDU 5920 Ugly Problem(模拟)
- HDU 5920 Ugly Problem(大数减法 模拟)
- hdu 5920 Ugly Problem(构造题)
- Ugly Problem HDU
- HDU5920 Ugly Problem(模拟,字符串)
- HDU 5920 Ugly Problem 【模拟】 (2016中国大学生程序设计竞赛(长春))
- HDU 5920 Ugly Problem CCPC长春赛区 贪心
- hdu 5920 Ugly Problem 贪心构造 大整数
- hdu 5920 Ugly Problem(CCPC长春,构造回文数)
- HDU 5920 Ugly Problem (JAVA高精度+回文数)
- HDU 5920 Ugly Problem (大数模拟 构造回文串)
- hdu 4920 Ugly Problem [模拟+大数减法]
- HDU 5918 Ugly Problem(模拟)
- HDOJ 5920 Ugly Problem 【模拟】
- Ugly Problem
- hdu1059 Dividing
- 质因数分解
- 时间慢些,我还没长大!
- C#对象工厂类
- Hive II
- hdu 5920 Ugly Problem 字符串处理
- 配置jupyter notebook-----更改工作目录
- 193. Valid Phone Numbers。
- bonding(绑定与删除)
- CSS3的REM设置字体大小
- [leetCode 解题报告]017. Letter Combinations of a Phone Number
- python装饰器与面向对象
- BFS和DFS的比较
- 基于Java、Kafka、ElasticSearch的搜索框架的设计与实现