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. jquery设置select选中

    /*设置select选中开始*/ var prod_type=$('.prod_type').val(); //alert(prod_type); var select = document.getE ...

  2. 【转发】彻底理解 JS 中 this 的指向

    为什么要学习this?如果你学过函数式编程,面向对象编程,那你肯定知道干什么用的,如果你没有学过,那么暂时可以不用看这篇文章,当然如果你有兴趣也可以看看,毕竟这是js中必须要掌握的东西. 例子1: f ...

  3. Java 序列化 对象序列化和反序列化

    Java 序列化 对象序列化和反序列化 @author ixenos 对象序列化是什么 1.对象序列化就是把一个对象的状态转化成一个字节流. 我们可以把这样的字节流存储为一个文件,作为对这个对象的复制 ...

  4. 8、Spring+Struts2+MyBaits(Spring注解+jdbc属性文件+log4j属性文件)

    一.注解理论 使用注解来构造IoC容器 用注解来向Spring容器注册Bean.需要在applicationContext.xml中注册<context:component-scan base- ...

  5. UVA 562 Dividing coins (01背包)

    //平分硬币问题 //对sum/2进行01背包,sum-2*dp[sum/2] #include <iostream> #include <cstring> #include ...

  6. java的String类型为什么是final

    (转自:http://www.cnblogs.com/ikuman/archive/2013/08/27/3284410.html) 最佳答案: 主要是为了“效率” 和 “安全性” 的缘故.若 Str ...

  7. ural 1013. K-based Numbers. Version 3(动态规划)

    1013. K-based Numbers. Version 3 Let’s consider K-based numbers, containing exactly N digits. We def ...

  8. Xcode最最实用快捷键

      转发:http://www.open-open.com/lib/view/open1397988745593.html 关于与mac 通用的快捷键网上太多,就不说了,下面介绍一些大家不是很熟悉但是 ...

  9. Android获取网络数据进行GZIP解压

    说明:现在很多网站都会在回传数据的时候进行GZIP压缩,我们可以在请求头中申明支持GZIP压缩.可以减轻网络传输压力,Xutils中已经实现. 下面是一个DEMO,便于理解. private void ...

  10. split分割函数

    java.lang.string.split split 方法 将一个字符串分割为子字符串,然后将结果作为字符串数组返回. stringObj.split([separator,[limit]]) 参 ...