描述

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. C语言编程学习:链表的来源分析

    C语言是面向过程的,而C++是面向对象的 C和C++的区别: C是一个结构化语言,它的重点在于算法和数据结构.C程序的设计首要考虑的是如何通过一个过程,对输入(或环境条件)进行运算处理得到输出(或实现 ...

  2. Live 直播过程

    采集.处理.编码.封包.推流.传输.转码.分发.拉流.解码.播放,从推流到播放

  3. Ajax GET

    $ajax的post请求提交方式: Controller: @RequestMapping("/emps") @ResponseBody public Msg getEmps(@R ...

  4. C# 抽象(2)

    接着上章说: 先看代码 abstract class Human { public abstract void Think(); public abstract int Age { get; set; ...

  5. 离线安装 python 第三方库

     离线安装 python 第三方库 首先你需要在联网的服务器上已经安装了一个第三方库,比如是paramiko,也就是说你已经执行了 pip install paramiko    ,小提示: 如果在安 ...

  6. React基础篇(2) -- state&props&refs

    内容简介 state props refs 行内样式及动态类名 state 基本介绍 React 把组件看成是一个状态机(State Machines).通过与用户的交互,实现不同状态,然后渲染 UI ...

  7. Jmeter_使用IE代理录制脚本

    因为项目登录的密码需要RSA加密,选用了jmeter作为压测工具: 就自己本次项目,顺便学习Jmeter,做一个简单的记录,本文主要介绍使用IE代理录制脚本: 自己也尝试过使用Badboy录制,还是喜 ...

  8. Windowns DOS For 循环实例

    update_all.bat代码示例: @echo off echo ***************************************************************** ...

  9. 【bzoj3576】[Hnoi2014]江南乐 数论分块+博弈论

    Description 小A是一个名副其实的狂热的回合制游戏玩家.在获得了许多回合制游戏的世界级奖项之后,小A有一天突然想起了他小时候在江南玩过的一个回合制游戏. 游戏的规则是这样的,首先给定一个数F ...

  10. loj#6436. 「PKUSC2018」神仙的游戏(NTT)

    题面 传送门 题解 一旦字符串踏上了通配符的不归路,它就永远脱离了温暖的字符串大家庭的怀抱 用人话说就是和通配符扯上关系的字符串就不是个正常的字符串了比如说这个 让我们仔细想想,如果一个长度为\(le ...