TOJ 4002 Palindrome Generator
描述
A palindrome is a number that reads the same whether you read it from left to right or from right to left. Here is a surprising fact. Suppose we start with a number n. We reverse the digits of n and add it to n. If this number is a palindrome we stop. Otherwise, we repeat the reversal and addition and continue. For many numbers this process leads us to a palindrome!
In this task you will be given a number n with at most 10 digits. Your task is to carry out the process described above and output the resulting palindrome, if you find one. However, if the number of digits exceeds 30 and you still have not found a palindrome then output -1.
For example if n is 149 then we get the following sequence: 149, 1090, 1991. Thus your program should output 1991 in this case. However, if we start with 196 we do not get a palindrome before the number of digits exceeds 30 and in this case the output should be -1.
输入
A single line with a single integer n, with not more than 10 digits.
You may assume that n has at most 10 digits.
输出
A single number that is either -1 or the palindrome generated by the process described above.
样例输入
149
样例输出
1991
题目来源
题目意思很简单,输出回文数。如果不是回文那么就把它反一反相加。继续判断,如果超过30位还不是回文就将它输出来。
注意是大数,所以使用数组存储。
#include <stdio.h> __int64 n;
int maxDigit;
int num[];
int renum[];
int out[]; bool palindrome(){
int k=maxDigit-;
for(int i=;i<maxDigit/; i++,k--){
if(out[i]!=out[k])return ;
}
return ;
} void getNum(){
int k=;
for(int i=; i<maxDigit; i++){
num[k++]=out[i];
}
} void getRenum(){
int k=;
for(int i=maxDigit-; i>=; i--){
renum[k++]=num[i];
}
} void plus(){
int carry=;
int cnt=;
int digit=;
for(int i=maxDigit-; i>= || carry!=; i--){
if(i>=){
out[cnt++]=(num[i]+renum[i]+carry)%;
carry=(num[i]+renum[i]+carry)/;
digit++;
}else{
out[cnt++]=carry%;
carry=carry/;
digit++;
}
}
maxDigit=digit;
} bool judge(){
int flag=;
//如果这个数不是回文
while(!palindrome()){
getNum();
getRenum();
plus();
if(maxDigit>){
flag=;
break;
}
}
return flag;
} int main(int argc, char *argv[])
{
while( scanf("%I64d",&n)!=EOF ){
int cnt=;
maxDigit=;
while(n!=){
out[cnt++]=n%;
n/=;
maxDigit++;
}
if(!judge()){
printf("-1\n");
}else{
for(int i=maxDigit-; i>=; i--){
printf("%d",out[i]);
}
printf("\n");
}
}
return ;
}
TOJ 4002 Palindrome Generator的更多相关文章
- 10453 Make Palindrome (dp)
Problem A Make Palindrome Input: standard input Output: standard output Time Limit: 8 seconds By def ...
- 周赛A题
A Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Description By d ...
- LightOJ 1033 Generating Palindromes(dp)
LightOJ 1033 Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- 1033 - Generating Palindromes
1033 - Generating Palindromes PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit ...
- Prime Palindrome Golf
Prime Palindrome Golf Do you know how to play Prime Palindrome Golf? You are given a number and your ...
- Digit Generator(水)
题目链接:http://acm.tju.edu.cn/toj/showp2502.html2502. Digit Generator Time Limit: 1.0 Seconds Memor ...
- EasyMesh - A Two-Dimensional Quality Mesh Generator
EasyMesh - A Two-Dimensional Quality Mesh Generator eryar@163.com Abstract. EasyMesh is developed by ...
- 轻量级“集合”迭代器-Generator
Generator是PHP 5.5加入的新语言特性.但是,它似乎并没有被很多PHP开发者广泛采用.因此,在我们了解PHP 7对Generator的改进之前,我们先通过一个简单却显而易见的例子来了解下G ...
- .NET平台开源项目速览(18)C#平台JSON实体类生成器JSON C# Class Generator
去年,我在一篇文章用原始方法解析复杂字符串,json一定要用JsonMapper么?中介绍了简单的JSON解析的问题,那种方法在当时的环境是非常方便的,因为不需要生成实体类,结构很容易解析.但随着业务 ...
随机推荐
- 来自网易云的黑科技,带尖角的div......
今天在网易云的网页版听歌,话说Steve Vai的曲子永远是这么让人揣摩不透,不过我还时更喜欢老Joe,咦,跑题了··· 大家可以看到评论输入框和回复框,上面都有个小尖角,实现的方式有很多,我一般是用 ...
- angular 子路由
const routes: Routes = [ { path: '', redirectTo: '/home', pathMatch: 'full' }, { path: 'home', compo ...
- C# 继承(2)
接着上章: 代码如下- class NameList { public string Name { get; set; } public void ID() => Console.WriteLi ...
- 在win7系统上搭建django+oracle 11g时,注意事项
在win7系统上搭建django+oracle 11g时,注意事项[示例用的是python 2.7]: 重要:python.oracle.oracle client这三个的OS bit 一定一定要相同 ...
- 配置Java web的一次经历
最近在完成数据库作业,重新拾起了以前学过的Java,讲下自己的 Java web 配置过程. 1.安装 Tomcat 在官网下载 Tomcat7.0版本:https://tomcat.apache.o ...
- 微服务架构下分布式事务解决方案——阿里云GTS
https://blog.csdn.net/jiangyu_gts/article/details/79470240 1 微服务的发展 微服务倡导将复杂的单体应用拆分为若干个功能简单.松耦合的服务,这 ...
- django 部署到Ubuntu-apache2遇到的问题汇总
1.x86_64-linux-gnu-gcc: error: unrecognized command line option ‘-fstack-protector-strong’ Turns out ...
- 【智能算法】变邻域搜索算法(Variable Neighborhood Search,VNS)超详细解析和TSP代码实例以及01背包代码实例
喜欢的话可以扫码关注我们的公众号哦,更多精彩尽在微信公众号[程序猿声] 00 目录 局部搜索再次科普 变邻域搜索 造轮子写代码 01 局部搜索科普三连 虽然之前做的很多篇启发式的算法都有跟大家提过局部 ...
- 2.2、Softmax Regression算法实践
Softmax Regression算法实践 有了上篇博客的理论知识,我们可以利用实现好的函数,来构建Softmax Regression分类器,在训练分类器的过程中,我们使用多分类数据作为训练数据: ...
- redux超易学三篇之三(一个逻辑完整的react-redux)
配合源代码学习吧~ : 我是源代码 这一分支讲的是 如何完整地(不包含优化,也没有好看的页面) 搭建一个 增删改查 的 react-redux 系统 不同于上一节的 react-redux,这里主要采 ...