传送门: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. Semantic UI – 完全语义化的前端界面开发框架

    Semantic UI 是一个 UI 库,使前端开发更简单,更容易学习.Semantic UI 介绍了许多界面元素.在大多数情况下,只有你需要的元素建立一个自定义的构建可能是最好的. UI 组件分为四 ...

  2. windows 安装 go语言

    下载地址:http://www.cnblogs.com/osfipin/p/4856839.html. 官方教程说明: Windows 对于Windows用户,Go项目提供两种安装选项(从源码安装除外 ...

  3. C#仿google日历asp.net简单三层版本

    网上搜了很多xgcalendar的例子都是Php开发的,而且官方站上的asp.net/MVC版 在vs10 08 都报错. 所以自己重新用三层写了一下希望对大家有帮助 废话不多说了 先看看它都有些什么 ...

  4. EXCEL经纬度转化

    =LEFT(A1,FIND("°",A1)-1)+MID(A1,FIND("°",A1)+1,FIND(CHAR(39),A1)-1-FIND("°& ...

  5. SharePoint如何关掉mysite. how to disable mysite creation

    一个很简单的问题 center admin --> application managment -->manage service application -->user profi ...

  6. Android通过PHP服务器实现登录

    Android客户端和PHP.MySQL搭建的服务器之间的简单交互,实现登录功能 . 实现原理图: Handler消息机制原理: Handler机制主要包括4个关键对象,分别是Message.Hand ...

  7. <转>最新版SDWebImage的使用

    我之前写过一篇博客,介绍缓存处理的三种方式,其中最难,最麻烦,最占内存资源的还是图片缓存,最近做的项目有大量的图片处理,还是采用了SDWebImage来处理,但是发现之前封装好的代码报错了.研究发现, ...

  8. iOS KVC详细讲解

    iOS KVC详细讲解 什么是KVC? KVC即NSKeyValueCoding,就是键-值编码的意思.一个非正式的 Protocol,是一种间接访问对象的属性使用字符串来标识属性,而不是通过调用存取 ...

  9. linux下使用yum安装mysql详解

    1.安装客户端和服务器端 确认mysql是否已安装: ? 1 2 yum list installed mysql* rpm -qa | grep mysql* 查看是否有安装包: ? 1 yum l ...

  10. css文本格式详解

    一.css文本主体内容: 二.css文本详解:  1.文本缩进 语法: text-indent:<length>|<percentage> 默认值为0. 属性值详解: < ...