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

关于HDOJ 1266题(Reverse Number) 的理解(C/C++)

2019-08-17 hdoj 数字逆序 HDU 2006-4 Programming Contest
Word count: 608 | Reading time: 3min


    本来应该是一个水题的,结果还是细节上有点小问题,之前WA的2次代码没有处理好中间0,例如输入502,应该输出205的,结果按之前的代码的话就直接给中间0那给截断了,只输出了一个两位数……所以肯定是WA了。之后注意了下中间0,就给AC了。
在这里也给出除了例题的另一组特殊数据吧,方便小伙伴们测试

input:

3
-0012560020
00000
00205



output:

-20065210
0
502



题目描述:Reverse Number(数字逆序)

Reverse Number
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/32768 K (Java/Others)

Problem Description
    Welcome to 2006’4 computer college programming contest!

Specially, I give my best regards to all freshmen! You are the future of HDU ACM! And now, I must tell you that ACM problems are always not so easy, but, except this one… Ha-Ha!

Give you an integer; your task is to output its reverse number. Here, reverse number is defined as follows:

  1. The reverse number of a positive integer ending without 0 is general reverse, for example, reverse (12) = 21;
  2. The reverse number of a negative integer is negative, for example, reverse (-12) = -21;
  3. The reverse number of an integer ending with 0 is described as example, reverse (1200) = 2100.

Input
    Input file contains multiple test cases. There is a positive integer n (n<100) in the first line, which means the number of test cases, and then n 32-bit integers follow.

Output
    For each test case, you should output its reverse number, one case per line.

Sample Input
3
12
-12
1200

Sample Output
21
-21
2100

Author

    lcy

Source

    HDU 2006-4 Programming Contest



原题链接

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
#include<iostream>
#include<cmath>
using namespace std;

long long int n,input_;
int main() {
cin >> n;
while (n--) {
cin>>input_;
long long int x = input_,a,count=0,num=0;
while (x) {
a = x % 10;
x /= 10;
num++;
}
x = input_; a = 10;
for (int i = 1; i <= num; i++) {
if (x % a) count++;
a *= 10;
}
long long int res = 0,k=num-1;
while (count) {
res += ((long long int)(input_ / pow(10, num - count ) )% 10) * pow(10, k);
k--; count--;
}
cout << res << endl;
}
return 0;
}

    ps.直接用int感觉应该也可以过吧,题目里貌似是说了int32位的数,我的AC代码只是因为之前WA时误以为是数据类型的锅,就给改成了long long int,有兴趣的小伙伴可以试试直接跑int。

参考博客

感谢 cnxo   的 HDOJ 1266 Reverse Number(数字反向输出题) 提示了我之前WA考略欠缺的地方(不过貌似他给的数据输出有点不对?已给老哥评论了,等回复)

Author: Zoey

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

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

< PreviousPost
关于HDOJ 1279题(验证角谷猜想) 的理解(C/C++)
NextPost >
算法笔记 前缀和 差分 快速幂(C/C++)
CATALOG
  1. 1. 题目描述:Reverse Number(数字逆序)
  2. 2. 原题链接
  3. 3. Accepted代码
  4. 4. 参考博客