描述

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

题目来源

TOJ

题目意思很简单,输出回文数。如果不是回文那么就把它反一反相加。继续判断,如果超过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的更多相关文章

  1. 10453 Make Palindrome (dp)

    Problem A Make Palindrome Input: standard input Output: standard output Time Limit: 8 seconds By def ...

  2. 周赛A题

    A  Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu   Description By d ...

  3. LightOJ 1033 Generating Palindromes(dp)

    LightOJ 1033  Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...

  4. 1033 - Generating Palindromes

    1033 - Generating Palindromes    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit ...

  5. Prime Palindrome Golf

    Prime Palindrome Golf Do you know how to play Prime Palindrome Golf? You are given a number and your ...

  6. Digit Generator(水)

    题目链接:http://acm.tju.edu.cn/toj/showp2502.html2502.   Digit Generator Time Limit: 1.0 Seconds   Memor ...

  7. EasyMesh - A Two-Dimensional Quality Mesh Generator

    EasyMesh - A Two-Dimensional Quality Mesh Generator eryar@163.com Abstract. EasyMesh is developed by ...

  8. 轻量级“集合”迭代器-Generator

    Generator是PHP 5.5加入的新语言特性.但是,它似乎并没有被很多PHP开发者广泛采用.因此,在我们了解PHP 7对Generator的改进之前,我们先通过一个简单却显而易见的例子来了解下G ...

  9. .NET平台开源项目速览(18)C#平台JSON实体类生成器JSON C# Class Generator

    去年,我在一篇文章用原始方法解析复杂字符串,json一定要用JsonMapper么?中介绍了简单的JSON解析的问题,那种方法在当时的环境是非常方便的,因为不需要生成实体类,结构很容易解析.但随着业务 ...

随机推荐

  1. TSQL--时间类型和毫秒数转换

    项目中使用BIGINT来存放时间,以下代码用来转换时间类型和BIGINT类型 SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ========= ...

  2. sqlServer2008技术内幕笔记总结(实用的sql方法总结)

    over函数的使用: 1.可以实现基于什么求和,省去group by: select xingming,xingbie,COUNT(*) over() as '总人数' from jbxx_xuesh ...

  3. html5 video微信浏览器视频不能自动播放

    html5 video微信浏览器视频不能自动播放 一.微信浏览器(x5内核): 1.不能自动播放 2.全屏 3.最顶层(z层的最顶层) 二.ios系统解决方案:(无phone手机未测试) <au ...

  4. spring 学习(二):spring bean 管理--配置文件和注解混合使用

    spring 学习(二)spring bean 管理--配置文件和注解混合使用 相似的,创建 maven 工程,配置pom.xml 文件,具体可以参考上一篇博文: sprint 学习(一) 然后我们在 ...

  5. iOS 开发之 GCD 不同场景使用

    header{font-size:1em;padding-top:1.5em;padding-bottom:1.5em} .markdown-body{overflow:hidden} .markdo ...

  6. 最近关于linux的一些小问题。

    redhat 用yum更新时需要注册付费.centos 不用. 原来版本的ifconfig 在centos中变为了ip addr.

  7. Hibernate的工作流程以及三种状态

    Hibernate的工作流程: 1. 读取并解析配置文件 2.读取并解析映射信息,创建SessionFactory 3. 打开Sesssion 4.创建事务Transation 5. 持久化操作 6. ...

  8. URL编码以及GET和POST提交乱码解决方案

    get方式时参数是作为url一部分传输的.而对于url是有规范的,汉字超出了规范的范围. post方式不需要加编码是错误的.post时也必须传送符合服务器规范的编码.如果错误,服务器收到的内容也不会正 ...

  9. 按钮重复点击问题 UIbutton

    .h #import <UIKit/UIKit.h> #import <objc/runtime.h> @interface UIControl (XY) @property ...

  10. DQL、DML、DDL、DCL的区别

    1. 历史发展 SQL(Structure Query Language)语言是数据库的核心语言. SQL的发展是从1974年开始的,其发展过程如下:1974年-----由Boyce和Chamberl ...