描述

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. ToastUtils

    public class ToastUtils { private static String TAG="ToastUtils"; private static Toast toa ...

  2. C# static 字段初始值设定项无法引用非静态字段、方法或属性

    问题:字段或属性的问题字段初始值设定项无法引用非静态字段.方法 下面代码出错的原因,在类中定义的字段为什么不能用? public string text = test(); //提示 字段或属性的问题 ...

  3. WebStrom常用快捷键

    查找替换 ctrl+shift+N  通过文件名快速查找工程内的文件(必记) ctrl+shift+alt+N  通过一个字符快速查找位置(必记) ctrl+F  在文件内快速查找代码 ctrl+R  ...

  4. Python3 + django2.0 + apache2 + ubuntu14部署网站上线

    自己尝试在本地搭建了 Django 项目后,想部署到自己云服务器上,经常多次尝试和多次踩坑(捂脸),总结如下: 环境:ubuntu14, django2.0, apache2. 1.首先安装需要的库包 ...

  5. C语言实现数组及链表的快速排序

    1. 数组快排: 方法一: #include <stdio.h> #include <stdlib.h> //交换 void swap(int *pi, int *pj) { ...

  6. django form tips

    1.form将获取的参数传递到field 2.field中的函数 to_python 数据库到python中变量 get_prep_value python变量到数据库 validate 验证,也可以 ...

  7. ExtJS 4.2.1学习笔记(一)——MVC架构与布局

    1       ExtJS入门 1.1     支持所有主流浏览器 调试推荐:chrome.Safari.Firefox 1.2     推荐目录结构 - appname (包含所有程序代码,是根目录 ...

  8. thinkphp3.2.3 批量包含文件

    自己瞎写的...凑合看吧...核心就是用正则 表达式 或者 字符串 str_replace 进行 替换....... /** * 批量包含---,不能递归包含!!! 请不要在目标目录 包含 文件夹,因 ...

  9. Bound Found(思维+尺取)

    Signals of most probably extra-terrestrial origin have been received and digitalized by The Aeronaut ...

  10. QDU_组队训练(AJFC)

    A - Pretty Matrix DreamGrid's birthday is coming. As his best friend, BaoBao is going to prepare a g ...