传送门: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. Slides - 在线制作效果精美的幻灯片(PPT)

    Slides 是可以在浏览器中使用的在线幻灯片编辑器.与传统的演示软件,比如 PowerPoint 相比,Slides 不需要下载任何东西.你所有的信息都是安全地存储在我们的服务器上,无论你在哪里.不 ...

  2. HTML中input标签的alt属性和title属性的比较

    经常用到这两个属性,但是一直没有总结他们的区别.现在我对他们两个的用法做一下总结: 相同点:他们都会飘出一个小浮层,显示文本内容. 不同点: 1.alt只能是元素的属性,而title即可以是元素的属性 ...

  3. ASP.NET程序中常用的三十三种代码

    1. 打开新的窗口并传送参数: 传送参数: response.write("<script>window.open(’*.aspx?id="+this.DropDown ...

  4. AE选中要素

    private void 选中要素ToolStripMenuItem_Click(object sender, EventArgs e) { if(axMapControl2.LayerCount&l ...

  5. Autodesk 360 Mobile不能显示图片?

    在6月21号的DevLab上,有一位朋友说Autodesk 360 Mobile在iPad上不能显示JPG图片预览.我当时没带iPad,不能测试.后天回家在Autodesk 360 Mobile 3. ...

  6. Linux下的应用程序性能分析 总结

    Linux下的应用程序性能分析,根据内核程序和应用程序的不同,下文分两类进行描述. 我们侧重的是应用级别的程序,推荐google perf tool/kcachegrind组合 一.和内核有关的工具 ...

  7. 【转】HttpClient使用Post和Get提交参数

    package httpclient; import java.io.IOException; import java.net.URLEncoder; import org.apache.common ...

  8. ubuntu 12.04 react-native 安装

    1.安装nodejs 和npm apt-get install nodejs apt-get install npm 2. 升级node js 和npm sudo npm cache clean -f ...

  9. iOS开发多线程篇—线程安全

    iOS开发多线程篇—线程安全 一.多线程的安全隐患 资源共享 1块资源可能会被多个线程共享,也就是多个线程可能会访问同一块资源 比如多个线程访问同一个对象.同一个变量.同一个文件 当多个线程访问同一块 ...

  10. 网络婚礼之AFNetWorking3.0

    目前使用人数最多的第三方网络库,没有之一.从开始的NSURLConnection到现在的NSURLSession,它都一直保持着与苹果的步调一致,而由它也衍生出大量的相关第三方网络功能库,不仅仅因为他 ...