传送门: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. [deviceone开发]-天气demo

    一.简介 该demo主要实现定位功能,读取天气信息,语音播报功能.其中定位需要配置key,调试二维码请到论坛中下载! 二.效果图 三.相关讨论 http://bbs.deviceone.net/for ...

  2. IE中的条件注释(转载自网络)

    IE条件注释是微软从IE5开始就提供的一种非标准逻辑语句,作用是可以灵活的为不同IE版本浏览器导入不同html元素,如:样式表,html标签等.很显然这种方法的最大好处就在于属于微软官方给出的兼容解决 ...

  3. 记STM32F030多通道ADC DMA读取乱序问题

    问题描述通过 uint16_t ConvData[8]保存DMA搬运的ADC转换数值,但是这个数组数值的顺序总是和ADC不是顺序对应的.比如用7个通道的ADC,当设置ADC_InitStructure ...

  4. ABAP 锁机制

  5. crm2011 使用SOAP 查询单个记录 Retrieve

    function getServiceUrl() {     var serverUrl = Xrm.Page.context.getServerUrl();     if (serverUrl.ma ...

  6. ORACLE -- ArcSDE Lock request conflicts with an established lock【转】

    具体的解决办法有如下三种 1.多半情况下关闭数据库连接可以解决这个问题,但有时候问题依然存在. 2. >1.关闭所有的ArcMap和ArcCatalog session. >2.开始——运 ...

  7. JavaSE 和 JavaEE 的关系

    1.javaSe看成是java基础   (Standard Edition(标准版) ) 2.J2EE看成是应用            (Enterprise Edition(企业版)) 3.java ...

  8. entityframework lamda 使用where时的注意事项

    我在项目中做了个底层 访问数据库泛型类 BaseEFDao<T> 在获取实体模型的时候使用了 Entities.CreateObjectSet<T>().Where(Func& ...

  9. Android项目实战(十九):Android Studio 优秀插件: Parcelable Code Generator

    Android Studio 优秀插件系列: Android Studio 优秀插件(一):GsonFormat Android Studio 优秀插件(二): Parcelable Code Gen ...

  10. c中的函数

    一. 什么是函数 l 任何一个C语言程序都是由一个或者多个程序段(小程序)构成的,每个程序段都有自己的功能,我们一般称这些程序段为“函数”.所以,你可以说C语言程序是由函数构成的. l 比如你用C语言 ...