hdu 1282 回文数猜想
来源:互联网 发布:数据库物理结构包括 编辑:程序博客网 时间:2024/06/05 07:38
Java代码:
import java.util.Scanner;public class Main {static int bytes[] = new int[40];static int temps[] = new int[40];static int n;public static void main(String[] args) {Scanner sc = new Scanner(System.in);while (sc.hasNext()) {n = sc.nextInt();String answer = "" + n;for (int i = 0; i < bytes.length; i++) {bytes[i] = 0;}int i = 0;while (n > 0) {bytes[i] = n % 10;n /= 10;i++;}int count = 0;while (!isPalindrome()) {// 拷贝数据for (int j = 0; j <= n; j++) {temps[j] = bytes[j];}// 计算for (int k = 0; k <= n; k++) {bytes[k] += temps[n - k];bytes[k + 1] += bytes[k] / 10;bytes[k] %= 10;}if (bytes[n] / 10 > 0) {bytes[n + 1] = bytes[n] / 10;n++;}count++;if (isPalindrome()) {break;}// 答案String str = "";for (int j = n; j >= 0; j--) {str += bytes[j];}answer += "--->" + str;}String str = "";for (int j = n; j >= 0; j--) {str += bytes[j];}System.out.println(count);answer += "--->" + str;System.out.println(answer);}}/* * isPalindrome-是否是回文数 */static boolean isPalindrome() {int i = bytes.length - 1;for (; i >= 0; i--) {if (bytes[i] != 0) {n = i;break;}}for (int j = 0; j * 2 <= i; j++) {if (bytes[j] != bytes[i - j]) {return false;}}return true;}}
回文数猜想
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5548 Accepted Submission(s): 3338
Problem Description
一个正整数,如果从左向右读(称之为正序数)和从右向左读(称之为倒序数)是一样的,这样的数就叫回文数。任取一个正整数,如果不是回文数,将该数与他的倒序数相加,若其和不是回文数,则重复上述步骤,一直到获得回文数为止。例如:68变成154(68+86),再变成605(154+451),最后变成1111(605+506),而1111是回文数。于是有数学家提出一个猜想:不论开始是什么正整数,在经过有限次正序数和倒序数相加的步骤后,都会得到一个回文数。至今为止还不知道这个猜想是对还是错。现在请你编程序验证之。
Input
每行一个正整数。
特别说明:输入的数据保证中间结果小于2^31。
特别说明:输入的数据保证中间结果小于2^31。
Output
对应每个输入,输出两行,一行是变换的次数,一行是变换的过程。
Sample Input
2722837649
Sample Output
327228--->109500--->115401--->219912237649--->132322--->355553
Author
SmallBeer(CML)
Source
杭电ACM集训队训练赛(VII)
Recommend
lcy | We have carefully selected several similar problems for you: 1287 1276 1256 1250 1230
0 0
- HDU-1282回文数猜想
- HDU 1282 回文数猜想
- HDU 1282 回文数猜想
- hdu-1282-回文数猜想
- hdu 1282 回文数猜想
- hdu 1282 回文数猜想
- HDU 1282 回文数猜想
- HDU-1282 回文数猜想
- 回文数猜想 HDU 1282
- HDU 1282 回文数猜想
- hdu 1282 回文数猜想
- hdu 1282 回文数猜想
- hdu 1282 回文数猜想
- hdu 1282 回文数猜想
- HDU 1282 回文数猜想
- HDU 1282 回文数猜想
- HDU 1282 回文数猜想
- HDU:回文数猜想
- 哈夫曼树与哈夫曼编码
- 制作Windows Server 2008安装启动U盘
- HDU_1850 Being a Good Boy in Spring Festival(Nim)
- VS2010 C++编译报错LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- 游戏计划3
- hdu 1282 回文数猜想
- Word Break II
- const、volatile、mutable关键字
- AFNetworking 上传多张图片 xml 解析方式
- Redis双链表实现安全队列
- GeekBand·iOS--Retain Cycle(引用循环)那些事
- 泛型(二)
- vector中erase()方法详解
- python学习第二天