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解析的问题,那种方法在当时的环境是非常方便的,因为不需要生成实体类,结构很容易解析.但随着业务 ...
随机推荐
- 将DataTable进行分页并生成新的DataTable
/// <summary> /// 将DataTable进行分页并生成新的DataTable /// </summary> /// <param name="d ...
- 远程连接linux服务上的mysql
如果有童鞋linux上还未安装mysql数据库可以参考我上一篇博客 (1)首先确保 linux服务上的 mysql 的3306端口是对外开放的 编辑 vi /etc/sysconfig/iptable ...
- Kotlin when 流程判断
如果学过C或者java C#等语言. 一定熟悉SWITCH这个流程判断 但是在kotlin中却没有这个.而是 使用了When来代替. 当什么时候. 下面我觉一个简单的例子: import java.u ...
- C语言宏的定义和宏的使用方法(#define)
1.宏的功能介绍 在 C 语言中,可以采用命令 #define 来定义宏.该命令允许把一个名称指定成任何所需的文本,例如一个常量值或者一条语句.在定义了宏之后,无论宏名称出现在源代码的何处,预处理器都 ...
- javascript 数组排序
var arr=[1,2,3,5,10,4,2,19,2,0]; alert(arr);//[1,2,3,5,10,4,2,19,2,0] arr.sort(function (a, b) {//升序 ...
- Redhat7无法启动mysql
是这样的,7的这个环境安装了叫MariaDB了 安装MariaDB之后必须先启动MariaDB [root@redhatx ~]# yum -y install mysql [root@redhatx ...
- Effective Java 3rd.Edition 翻译
推荐序 前言 致谢 第一章 引言 第二章 创建和销毁对象 第1项:用静态工厂方法代替构造器 第2项:遇到多个构造器参数时要考虑使用构建器 第3项:用私有构造器或者枚举类型强化Singleton属性 第 ...
- 三种timer控件的简单实例
.system.windows.forms .system.threading.timer .system.timers.timer using System; using System.Collec ...
- Freeman链码
[简介] 链码(又称为freeman码)是用曲线起始点的坐标和边界点方向代码来描述曲线或边界的方法,常被用来在图像处理.计算机图形学.模式识别等领域中表示曲线和区域边界.它是一种边界的编码表示法,用边 ...
- ELK 实用架构