Ugly Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
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.

 
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).

 
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
2
18
1000000000000
 
Sample Output
Case #1:
2
9
9
Case #2:
2
999999999999
1

Hint

9 + 9 = 18
999999999999 + 1 = 1000000000000

分析:找一个较大回文数,然后大数相减;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<ll,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
const int maxn=1e3+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t,cnt,cas;
char a[maxn],ans[][maxn];
string gao(string a, string b){
int lena = a.length();
int lenb = b.length();
int len = max(lena, lenb) + ;
char res[len];
memset(res, , sizeof(res));
int flag, reslen = len;
len--;
res[len] = ;
len--;
lena--; lenb--;
flag = ;
while(lenb >= ){
res[len] = a[lena--] - b[lenb--] + '' - flag;
flag = ;
if(res[len] < ''){
flag = ;
res[len] = res[len] + ;
}
len--;
}
while(lena >= ){
res[len] = a[lena--] - flag;
flag = ;
if(res[len] < ''){
flag = ;
res[len] = res[len] + ;
}
len--;
}
while((res[flag] == || res[flag] == '') && flag < reslen) flag++;
if(flag == reslen) return "";
return res + flag;
}
void find(char*p,int now)
{
int i,len=strlen(p);
ans[now][len]=;
for(int i=;i<(len+)/;i++)
ans[now][i]=ans[now][len--i]=p[i];
bool flag=false;
for(int i=len/-;i>=;i--)
{
if(p[i]<p[len--i])break;
else if(p[i]>p[len--i])
{
ans[now][i]--;
ans[now][len--i]--;
for(int j=i+;j<len--i;j++)ans[now][j]=ans[now][len--j]='';
break;
}
}
for(i=;ans[now][i]=='';i++)
{
ans[now][len--i]='';
}
strcpy(ans[now],ans[now]+i);
strcpy(a,gao(p,ans[now]).c_str());
}
int main()
{
int i,j;
scanf("%d",&t);
while(t--)
{
cnt=;
scanf("%s",a);
while(strcmp(a,"")!=)
{
find(a,cnt);
cnt++;
}
printf("Case #%d:\n%d\n",++cas,cnt);
rep(i,,cnt-)printf("%s\n",ans[i]);
}
//system("Pause");
return ;
}
 

Ugly Problem的更多相关文章

  1. hdu-5920 Ugly Problem(贪心+高精度)

    题目链接: Ugly Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  2. HDU 5920 Ugly Problem 【模拟】 (2016中国大学生程序设计竞赛(长春))

    Ugly Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  3. D - Ugly Problem HDU - 5920

    D - Ugly Problem HDU - 5920 Everyone hates ugly problems. You are given a positive integer. You must ...

  4. 2016中国大学生程序设计竞赛(长春) Ugly Problem 模拟+大数减法

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5920 我们的思路是: 对于一个串s,先根据s串前一半复制到后一半构成一个回文串, 如果这个回文串比s小, ...

  5. HDU 5920 Ugly Problem

    说起这道题, 真是一把辛酸泪. 题意 将一个正整数 \(n(\le 10^{1000})\) 分解成不超过50个回文数的和. 做法 构造. 队友UHC提出的一种构造方法, 写起来比较方便一些, 而且比 ...

  6. HDU 5920 Ugly Problem 高精度减法大模拟 ---2016CCPC长春区域现场赛

    题目链接 题意:给定一个很大的数,把他们分为数个回文数的和,分的个数不超过50个,输出个数并输出每个数,special judge. 题解:现场赛的时候很快想出来了思路,把这个数从中间分为两部分,当位 ...

  7. HDU - 5920 Ugly Problem 求解第一个小于n的回文数

    http://acm.hdu.edu.cn/showproblem.php?pid=5920 http://www.cnblogs.com/xudong-bupt/p/4015226.html 把前半 ...

  8. TZOJ 4021 Ugly Problem(线段树区间子段最大)

    描述 给定一个序列A[0],A[1],…A[N-1],要求找到p0,p1,p2,p3使得A[p0]+A[p0+1]+…+A[p1] + A[p2]+A[p2+1]+…+A[p3]最大(0<=p0 ...

  9. 2016中国大学生程序设计竞赛(长春)-重现赛 1010Ugly Problem 回文数 模拟

    Ugly Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

随机推荐

  1. GlusterFS无法启动原因及处理方案

    启动结果: Redirecting to /bin/systemctl status  glusterd.serviceglusterd.service - GlusterFS, a clustere ...

  2. WCF、Web API、WCF REST、Web Service的区别

    Difference between WCF and Web API and WCF REST and Web Service   The .Net framework has a number of ...

  3. txt文件的读取

    两个函数:textread或importdata [textread函数] 格式:I=textread('文件名.txt','列',读取的行数,'headerlines',跳过的头行数);  返回值I ...

  4. NSConditionLock

    一.NSConditionLock定义了一个可以指定条件的互斥锁,用于线程之间的互斥与同步. 这里的条件并不是bool表达式中的条件,而是一个特定的int值. 二.NSConditionLock的AP ...

  5. Zeppelin添加mysql解释器

    安装Apache zeppelin 1 wget http://apache.fayea.com/zeppelin/zeppelin-0.6.2/zeppelin-0.6.2-bin-all.tgz ...

  6. HDU 1509 Windows Message Queue(队列)

    题目链接 Problem Description Message queue is the basic fundamental of windows system. For each process, ...

  7. HDU 1863 Kruskal求最小生成树

    好久没写博客了写着玩的…… Kruskal这种东西离散都学过…… 一句话…… 添加当前图权值最小且构不成环的一条边 直到连接所有点…… 其他人好多Kruskal的模版 肯定有比我的好的…… 就是刷一波 ...

  8. linode digitalocean哪个更好

    大多数人纠结的品牌是Linode和DigitalOcean.我有幸使用过两者的产品,从2011年起,我就在用Linode VPS套餐,2013年开始,我订购了一批DigitalOcean产品,下面说下 ...

  9. shell 之awk 关联数组高级应用

    最近由于数据迁移过,有些用户信息需要再次确认下,也许数据量比较大,但是需要最终确认的比如说是用户ID和其对应的用户积分数,这样就会导致出现文本a(老的数据),文本b(新的数据).比如 这是文本a.tx ...

  10. 高精度运算专题3-乘法运算(The multiplication operation)

    这个专题呢,我就来讲讲高精度的乘法,下面是三个计算乘法的函数,第一个函数是char类型的,要对字符串进行数字转换,而第二个是两个int类型的数组,不用转换成数字,第三个则更为优化,用a数组-b数组放回 ...