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. java.lang.SecurityException:Invalid signature file digest forManifest main attributes

    今天在公司使用ant命令来从服务器中提取一个类的时候发现一个问题,每当我执行ant命令的时候就报如下异常: 想来想去猜测是Enovia提供的包没有做数字签名,但是如何解决这个问题呢?想不到解决方案只好 ...

  2. UESTC 1272 Final Pan's prime numbers(乱搞)

    题目链接 Description Final Pan likes prime numbers very much. One day, he want to find the super prime n ...

  3. BASE2(matlab)

    %{ // %} clc % linspace(3,5) 3到5 分成100 default %{ a=1 b=2 str = [num2str(a),'+',num2str(b)] eval(str ...

  4. oracle 11g高级 安装图解(摘自网络)

    使用的高级安装 启动OUI后出现“选择安装方式”窗口,我们选择:高级安装 步骤3:出现“选择安装类型”窗口,选择我们需要安装的版本.我们在此肯定是选择企业版. 至于产品语言不用选择,它会根据当前系统的 ...

  5. mongodb安装 win7版

    首先下载mongdb的zip包,可以到:http://www.mongodb.org/downloads.选择32位或64位的. 在D盘创建mongodb文件夹,d:\mongodb.把解压里面所有的 ...

  6. javascript焦点图自动缓冲滚动

    html中调用的js库,之前的随笔中有写,就不细说了,不明白的可以留言给我 <!DOCTYPE html> <html> <head> <meta chars ...

  7. 关于oracle数据库(2)

    数据备份.数据删除.数据还原 连接数据库,查看scott用户下面的所有表 数据备份(数据导出)要输入导出文件路径和文件名(文件扩展名可输入也可以不输入) 导出成功后,可以在上面输入的文件路径下面看到导 ...

  8. 在64位Win7操作系统中安装Microsoft Access Engine的解决方案

    在64位Win7操作系统中安装Microsoft Access Engine的解决方案 现在的Win7系统中安装的一般都是32位的Office,因为微软推荐使用32位的Office,兼容性更强,稳定性 ...

  9. DirectShow音频采集声音不连续问题分析与解决办法经验总结

    最近广州大雨不断,并且多数无前兆,突然就来场大雨,给同学们降降温,说来本也是好事,但有时候下的真不是时候,最近这段时间都是即将下班了,大雨就来了,昨晚快下班前又出现了大雨,北方人总爱忘带雨伞,这不就被 ...

  10. 关于UIFont和计算字符串的高度和宽度

    转自:http://i.cnblogs.com/EditPosts.aspx?opt=1 1.创建方法:+ fontWithName:size:- fontWithSize:2.创建系统字体:+ sy ...