VIRTUALS

the virtual labs for the virtuals

0%

HJ11. 数字颠倒

摘要:
Integer to String reverse.

反转整数

将数字每一位to string,逆序保存。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
* author: etoa
* 2021-08-18 17:56:33
*/
#include <bits/stdc++.h>

using namespace std;

int main()
{
int n;
cin >> n;
string s = n ? "" : "0";
for (; n; n /= 10) {
s += n % 10 + 0x30;
}
cout << s << endl;
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
* author: etoa
* 2021年08月18日18:13:59
*/
import java.util.*;

public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
StringBuilder sb = n == 0 ? new StringBuilder("0") : new StringBuilder();
for (; n != 0; n /= 10) {
sb.append((char)(n % 10 + 0x30));
}
System.out.println(sb.toString());
}
}

原题链接: HJ11. 数字颠倒