传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5920

我们的思路是:

  1. 对于一个串s,先根据s串前一半复制到后一半构成一个回文串,
  2. 如果这个回文串比s小,则做减法并递归;
  3. 如果相等直接结束;
  4. 如果大,那么找前一半离中心最近的一个非零数减1,并把这位之后到中心的数全都变为9,例如11000->11011,大了,所以变成10901;

ps:因为大数相减要传指针参数,调了蛮久,发现指针的乱指了,所以开了一个二维数组,每一次算出的原串和回文串都用新的指针,也就是s[tot],ss[tot],temp[tot];

/**************************************************************
Problem:
User: youmi
Language: C++
Result: Accepted
Time:
Memory:
****************************************************************/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
//#include<bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <cmath>
#include <queue>
#include <deque>
#include <string>
#include <vector>
#define zeros(a) memset(a,0,sizeof(a))
#define ones(a) memset(a,-1,sizeof(a))
#define sc(a) scanf("%d",&a)
#define sc2(a,b) scanf("%d%d",&a,&b)
#define sc3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define scs(a) scanf("%s",a)
#define sclld(a) scanf("%I64d",&a)
#define pt(a) printf("%d\n",a)
#define ptlld(a) printf("%I64d\n",a)
#define rep(i,from,to) for(int i=from;i<=to;i++)
#define irep(i,to,from) for(int i=to;i>=from;i--)
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define lson (step<<1)
#define rson (lson+1)
#define eps 1e-6
#define oo 0x3fffffff
#define TEST cout<<"*************************"<<endl
const double pi=*atan(1.0); using namespace std;
typedef long long ll;
template <class T> inline void read(T &n)
{
char c; int flag = ;
for (c = getchar(); !(c >= '' && c <= '' || c == '-'); c = getchar()); if (c == '-') flag = -, n = ; else n = c - '';
for (c = getchar(); c >= '' && c <= ''; c = getchar()) n = n * + c - ''; n *= flag;
}
ll Pow(ll base, ll n, ll mo)
{
ll res=;
while(n)
{
if(n&)
res=res*base%mo;
n>>=;
base=base*base%mo;
}
return res;
}
//*************************** int n;
const int maxn=+;
const ll mod=;
char ans[][maxn];
int tot=; int Minus(char *source1, char *source2, char *result)//返回:1(s1>s2),-1(s1<s2),0(s1=s2)
{
int length1 = strlen(source1); // 被减数的长度
int length2 = strlen(source2); // 减数的长度
int i, j, k = ;
int temp; // 临时存放位相减的结果
int bit = ; // 借位,1表示需要借位,0表示不需要借位
char ch; // 用于交换
for (i = length1 - , j = length2 - ; i >= && j >= ; --i, --j)
{
// 计算两个位之间的差值,同时要考虑借位
temp = (source1[i] - '') - (source2[j] - '') - bit; if (temp < ) // 需要借位
{
bit = ;
result[k++] = temp + + '';
}
else// 不需要借位
{
bit = ;
result[k++] = temp + '';
}
} while (i >= ) // length1 > length2的情况,结果为正数,将剩余数据赋值给计算结果数组
{
temp = source1[i--] - '' - bit;
if (temp < ) // 需要借位
{
bit = ;
result[k++] = temp + + '';
}
else
{
bit = ;
result[k++] = temp + '';
}
}
while (j >= )// length1 < length2的情况,结果为负数,将剩余数据赋值给计算结果数组
{
temp = - bit - (source2[j--] - '');
result[k++] = temp + '';
} // 对仍有进位的情况考虑,主要分两种:一种是strlen(p1)<strlen(p2),另一种是p1-p2<0,这两种情况bit为1
if (bit == )
{
// 最低位肯定不会被借位,所以不需要减去借位
// 只会向高位借位
result[] = - (result[] - '') + '';
for (i = ; i < k; i++)
{
result[i] = - (result[i] - '') - bit + '';
}
} for (i = k - , j = ; i >= j; --i, ++j)
{
ch = result[i];
result[i] = result[j];
result[j] = ch;
}
result[k] = '\0';
while(result[]==''&&strlen(result)>)//去掉前导零
strcpy(result,result+);
if(length1<length2)
return -;
else if(length1>length2)
return ;
else
{
for(int i=;i<length1;i++)
{
if(source1[i]>source2[i])
return ;
else if (source1[i]<source2[i])
return -;
}
}
return ;
}
char ss[][maxn],temp[][maxn];
char s[][maxn];
void dfs()
{
int len=strlen(s[tot]);
if(len==)
{
strcpy(ans[tot],s[tot]);
tot++;
return;
}
int half=len/;
int pre=-;
int num=-len%;
if(len%==)
{
for(int i=;i<half;i++)
{
if(s[tot][i]!='')
pre=max(pre,i);
ss[tot][i]=s[tot][i];
}
for(int i=half;i<len;i++)
ss[tot][i]=ss[tot][*half-i-];
}
else
{
for(int i=;i<=half;i++)
{
if(s[tot][i]!='')
pre=max(pre,i);
ss[tot][i]=s[tot][i];
}
for(int i=half+;i<len;i++)
ss[tot][i]=ss[tot][*half-i];
}
ss[tot][len]='\0';
s[tot][len]='\0';
int flag=Minus(s[tot],ss[tot],temp[tot]);
if(flag==-)
{
if(s[tot][pre]==''&&pre==)
{
for(int i=;i<len-;i++)
ss[tot][i]='';
ss[tot][len-]='\0';
}
else
{
ss[tot][pre]=s[tot][pre]-;
ss[tot][*half-pre-num]=s[tot][pre]-;
for(int i=pre+;i<*half-pre-num;i++)
ss[tot][i]='';
}
Minus(s[tot],ss[tot],temp[tot]);
strcpy(ans[tot],ss[tot]);
strcpy(s[tot+],temp[tot]);
tot++;
dfs();
}
else if(flag==)
{
strcpy(ans[tot],ss[tot]);
tot++;
return;
}
else
{
strcpy(ans[tot],ss[tot]);
strcpy(s[tot+],temp[tot]);
tot++;
dfs();
}
}
int main()
{
int kase=;
sc(n);
while(n--)
{
printf("Case #%d:\n",++kase);
tot=;
scs(s[tot]);
while(s[tot][]==''&&strlen(s[tot])>)//去掉前导零
strcpy(s[tot],s[tot]+);
dfs();
pt(tot);
rep(i,,tot-)
puts(ans[i]);
}
return ;
}

2016中国大学生程序设计竞赛(长春) Ugly Problem 模拟+大数减法的更多相关文章

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

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

  2. HDU 5916 Harmonic Value Description 【构造】(2016中国大学生程序设计竞赛(长春))

    Harmonic Value Description Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  3. HDU 5912 Fraction 【模拟】 (2016中国大学生程序设计竞赛(长春))

    Fraction Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  4. HDU 5914 Triangle 【构造】 (2016中国大学生程序设计竞赛(长春))

    Triangle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  5. 2016中国大学生程序设计竞赛 - 网络选拔赛 C. Magic boy Bi Luo with his excited tree

    Magic boy Bi Luo with his excited tree Problem Description Bi Luo is a magic boy, he also has a migi ...

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

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

  7. 2016中国大学生程序设计竞赛 - 网络选拔赛 1001 A water problem (大数取余)

    Problem Descripton Two planets named Haha and Xixi in the universe and they were created with the un ...

  8. 2016中国大学生程序设计竞赛 - 网络选拔赛 J. Alice and Bob

    Alice and Bob Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  9. 2016中国大学生程序设计竞赛 网络选拔赛 I This world need more Zhu

    This world need more Zhu Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

随机推荐

  1. 15款加速 Web 开发的 JavaScript 框架

    JavaScript 可以通过多种方式来创建交互式的网站和 Web 应用程序.利用 JavaScript,可以让你移动 HTML 元素,创建各种各样的自定义动画,给你的访问者更好的终端用户体验. 对于 ...

  2. Cool!15个创意的 CSS3 文本效果【下篇】

    这里文章收集了15个创意的 CSS3 文本效果,所有的都是精心挑选,这些可能会增加创意的火花到你的下一个项目.其中有些是用于特定用途,而另一些则适用于多种用途.如果你想要一个精彩而又充满色彩的文字效果 ...

  3. #8.10.16总结# 属性选择符 伪对象选择符 CSS的常用样式

    属性选择符 E[att] E[att="val"] E[att~="val"] E[att^="val"] E[att$="val ...

  4. 每次点击按钮后,判断页面是否已经有该行,没有弹出repeater的一行,并给他赋一个这行附值,没有则跳出

    protected void btnAdd_click(object sender, EventArgs e) { try { //记录第几次追加 pressCount++; typeString.A ...

  5. Autodesk 360 Viewer 已经发布到Autodesk 360平台

    我们之前已经在小范围内透露过,Autodesk 将发布一款完全无需插件的三维模型浏览器 Autodesk 360 Viewer,比如我们的Meetup线下小聚会,或者黑客马拉松(hackathon)的 ...

  6. clang LLVM 介绍和安装(Ubuntu10 64位)

    http://www.csdn.net/article/2013-11-27/2817632 的对Stanley B.Lippman采访提到clang的一些优点,以前程序员杂志也写过,为了提高系统的性 ...

  7. iOS启动过程

    1.main函数 | 2.UIApplicationMain      * 创建UIApplication对象      * 创建UIApplication的delegate对象 | 3.delega ...

  8. ios 取出subviews中指定subview

    for(UIView *view in subviews){ if(view.tag == 998) { //根据tag判断 } if([view isKindOfClass:[UIImageView ...

  9. 【Android】用HandlerThread模拟AsyncTask功能(ThreadTask)

    前言 AsyncTask是个好东西,能处理绝大多数应用线程和更新UI的任务,由于其内部使用了静态线程池,如果你有一堆异步任务(例如全局定时更新数据.同一个Activity中多个AsyncTask同时执 ...

  10. 【VLC-Android】Mac下编译vlc-android

    前言 突然想整整VLC-Android,然后就下一个玩玩看,这里记录点遇到的问题. 声明 欢迎转载,但请保留文章原始出处:)  博客园:http://www.cnblogs.com 农民伯伯: htt ...