zoey
点击国内主页可以让浏览速度加快哦 ~ !

关于HDOJ 1282题(回文数猜想) 的理解(C/C++)

2019-08-17 hdoj 粗心 回文 杭电ACM集训队训练赛(VII)
Word count: 562 | Reading time: 2min


    emmmmmm,是一道水题,但是粗心了,“==”写成了“=”,还去排了半天的bug……以后一定一定写成“常量==变量”,这样少写一个“=”的话就会报错了





题目描述:回文数猜想

回文数猜想
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/32768 K (Java/Others)

Problem Description
    一个正整数,如果从左向右读(称之为正序数)和从右向左读(称之为倒序数)是一样的,这样的数就叫回文数。任取一个正整数,如果不是回文数,将该数与他的倒序数相加,若其和不是回文数,则重复上述步骤,一直到获得回文数为止。例如:68变成154(68+86),再变成605(154+451),最后变成1111(605+506),而1111是回文数。于是有数学家提出一个猜想:不论开始是什么正整数,在经过有限次正序数和倒序数相加的步骤后,都会得到一个回文数。至今为止还不知道这个猜想是对还是错。现在请你编程序验证之。

Input
    每行一个正整数。
特别说明:输入的数据保证中间结果小于2^31。

Output
    对应每个输入,输出两行,一行是变换的次数,一行是变换的过程。

Sample Input
27228
37649

Sample Output
3
27228—>109500—>115401—>219912
2
37649—>132322—>355553

Author

    SmallBeer(CML)

Source

    杭电ACM集训队训练赛(VII)



原题链接

More info:Question




Accepted代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<iostream>
#include<cmath>
using namespace std;
int input_, num;
int reload(int x) {
int a = x,count=0,res=0,b;
while (a) {
a /= 10;
count++;
}
//cout << "the count of this number= " << count << endl;
a = x;
for (int i = 1; i <= count; i++) {
b=a % 10;
a /= 10;
res += (b * pow(10, count - i));
}
return res;
}
bool is_re(int x) {
if (reload(x) == x) return true;//就是这里少写一个=,写成了x=reload(x)
else {
return false;
}
}
int main() {
while (cin>>input_) {
num = 0;
int c = input_;
while (!is_re(c)) {
c += reload(c);
num++;
}
cout << num << endl; cout << input_;
while (!is_re(input_)) {
input_ += reload(input_);
cout << "--->" << input_;
}
cout << endl;
}
return 0;
}

参考博客

Author: Zoey

Link: https://zoey1038569979.github.io/2019/08/17/hdoj1282/

Copyright: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.

< PreviousPost
关于HDOJ 1283题(最简单的计算机) 的理解(C/C++)
NextPost >
关于HDOJ 1279题(验证角谷猜想) 的理解(C/C++)
CATALOG
  1. 1. 题目描述:回文数猜想
  2. 2. 原题链接
  3. 3. Accepted代码
  4. 4. 参考博客