code

Reverse Numbe Question( length 3)

1
2
3
4
5
6
7
8
9
10
11
12
任务描述
任务:编写程序,输入一个三位正整数,程序逆序输出该整数。

例如,输入是123,逆序输出该整数,即321。

测试说明
对编写的代码进行测试,并且约定输入的是一个三位正整数,输出的也是一个三位正整数,数字中间都没有空格。

测试输入:
123
预期输出:
321

0.Pseudocode

1
2
3
4
5
6
7
8
9
ALIGROTHM reverseThreeDigitNumber
INPUT: Integer n
Begin
units = n MOD 10
tens = (n/10) MOD 10
hundreds = n/10

OUTPUT units,tens,hundreds
END

标准版伪代码

1
2
3
4
5
6
7
8
9
10
ALGORITHM reverseThreeDigitNumber
INPUT: n (An integer)
OUTPUT: Reversed integer
BEGIN
units <- n MOD 10
tens <- (n/10) MOD 10
hundreds <- n/10

OUTPUT units,tens,hundreds
END

1.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>

int main()
{
/********* Begin *********/
printf("input three number:");
int n;
if (scanf("%d",&n))
{
int units = n % 10; // 提取个位
int tens = (n / 10) % 10; // 提取十位
int hundreds = n / 100; // 提取百位

// 按照 321 的顺序输出,中间不加空格
printf("%d%d%d\n",units,tens,hundreds);
}
/********* End *********/
return 0;
}

2.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

int main()
{
/********* Begin *********/
std::cout << "input three number:";
int n;
if (cin >> n) {
int units = n % 10; // 提取个位
int tens = (n / 10) % 10; // 提取十位
int hundreds = n / 100; // 提取百位

// 按照 321 的顺序输出,中间不加空格
std::cout << units << tens << hundreds << std::endl;
}
/********* End *********/
return 0;
}

使用标准算法库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
#include <algorithm>

int main(int argc, char* argv[]) {
// 检查是否有参数传入
if (argc < 2) return 1;

// 获取传过来的第一个参数
std::string input = argv[1];

// 执行核心算法:逆序
std::reverse(input.begin(), input.end());

// 输出给 PHP 接收
std::cout << input;

return 0;
}

3.assembly

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
section .data
; 定义输出格式:"百 十 个" 加上换行符 (0xA)
result db '0 0 0', 10
resultLen equ $ - result

section .text
global _start

_start:
; --- 初始数值准备 ---
mov eax, 482 ; 以 482 为例
mov ebx, 10 ; 除数

; --- 提取个位 (units) ---
xor edx, edx ; 清空 edx,准备 div (edx:eax / ebx)
div ebx ; eax = 48, edx = 2
add dl, '0' ; 转换为 ASCII 字符
mov [result + 4], dl ; 写入缓冲区第五个位置

; --- 提取十位 (tens) ---
xor edx, edx
div ebx ; eax = 4, edx = 8
add dl, '0' ; 转换为 ASCII 字符
mov [result + 2], dl ; 写入缓冲区第三个位置

; --- 提取百位 (hundreds) ---
add al, '0' ; 此时 eax 剩下的商就是百位数 4
mov [result], al ; 写入缓冲区第一个位置

; --- 使用 syscall (sys_write) 输出 ---
; rax = 1 (sys_write), rdi = 1 (stdout), rsi = addr, rdx = len
mov rax, 1
mov rdi, 1
lea rsi, [result]
mov rdx, resultLen
syscall

; --- 使用 syscall (sys_exit) 退出 ---
; rax = 60 (sys_exit), rdi = 0 (exit code)
mov rax, 60
xor rdi, rdi
syscall

linux下编译命令

1
2
nasm -f elf64 rdn.asm -o rdn.o
ld rdn.o -o rdn

4.pascal

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
program ReverseNumber;
var
n, units, tens, hundreds: integer;

begin
{ 1. 从键盘读取输入 }
write('请输入一个三位数: ');
readln(n);

{ 2. 核心逻辑处理 }
units := n mod 10; { 提取个位 }
tens := (n div 10) mod 10; { 提取十位 }
hundreds := n div 100; { 提取百位 }

{ 4. 输出结果 }
writeln(units,tens,hundreds);
end.

5.python

1
2
3
4
5
6
7
8
9
10
11
12
# 1. 获取输入并转换为整数
print("input three number:")
n = int(input())

# 2. 核心逻辑:利用整除 (//) 和 取余 (%)
units = n % 10 # 提取个位
tens = (n // 10) % 10 # 提取十位 (注意 Python 中整除使用 //)
hundreds = n // 100 # 提取百位

# 3. 输出结果
# print 默认会换行,且可以用 sep='' 取消参数间的空格
print(units, tens, hundreds, sep='')

方法二:字符串切片法(Python 炫技写法)

在 Python 工程师的日常工作中,如果只是单纯为了逆序输出,通常会把数字当成字符串处理。

Python

1
2
# 读取输入字符串,切片逆序 [::-1],然后输出
print(input()[::-1])

6.javascript

  1. node版
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
const readline = require('readline');

// 创建接口来处理标准输入和输出
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});

/********* Begin *********/
// 1. 输出提示文字 (对应 std::cout << "input three number:")
rl.question("input three number: ", (answer) => {
let n = parseInt(answer);

if (!isNaN(n)) {
// 2. 核心逻辑:提取各位数字
// 注意:JS 除法结果带小数,必须用 Math.floor 取整
let units = n % 10;
let tens = Math.floor(n / 10) % 10;
let hundreds = Math.floor(n / 100);

// 3. 输出结果 (对应 std::cout << units << tens << hundreds)
console.log(`${units}${tens}${hundreds}`);
}

rl.close();
});
/********* End *********/
  1. 浏览器控制台运行版 (F12 Console)

打开浏览器的开发者工具(F12),直接在 Console 面板粘贴以下代码。由于 JavaScript 在控制台没有像 cin 那样的同步阻塞读取功能,通常使用 prompt 来获取输入:

JavaScript

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/********* Begin *********/
// 1. 弹出输入框(对应 std::cout << "input three number:" 和 cin >> n)
let answer = prompt("input three number:");

if (answer !== null) {
let n = parseInt(answer);

// 2. 核心逻辑:提取各位数字
// JS 必须用 Math.floor 处理整除,否则 123/10 会变成 12.3
let units = n % 10;
let tens = Math.floor(n / 10) % 10;
let hundreds = Math.floor(n / 100);

// 3. 按照 321 的顺序输出到控制台
console.log(`${units}${tens}${hundreds}`);
}
/********* End *********/
  1. 纯前端 HTML 交互版

如果你想做一个网页来实现它,逻辑是这样的:

HTML

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<script>
// 逻辑部分
function reverseNumber() {
let n = parseInt(document.getElementById("numInput").value);

let units = n % 10;
let tens = Math.floor(n / 10) % 10;
let hundreds = Math.floor(n / 100);

document.getElementById("result").innerText = `${units}${tens}${hundreds}`;
}
</script>

<p>input three number:</p>
<input type="number" id="numInput" oninput="reverseNumber()">
<p>Result: <span id="result"></span></p>

7.java

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
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
// 1. 创建扫描器对象用于获取键盘输入
Scanner sc = new Scanner(System.in);

/********* Begin *********/
System.out.print("input three number:");

// 判断是否有输入,增强代码健壮性
if (sc.hasNextInt()) {
int n = sc.nextInt();

// 2. 核心数学逻辑
// Java 中 int / int 结果仍为 int,会自动取整(同 C++)
int units = n % 10; // 提取个位
int tens = (n / 10) % 10; // 提取十位
int hundreds = n / 100; // 提取百位

// 3. 按照逆序输出
// 注意:Java 中使用 System.out.print 不换行,println 换行
System.out.print(units);
System.out.print(tens);
System.out.println(hundreds);
}
/********* End *********/

sc.close(); // 养成良好习惯:关闭资源
}
}

8.c#

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
using System;

public class Program
{
public static void Main(string[] args)
{
/********* Begin *********/
Console.Write("input three number:");

// 1. 读取输入并转换为整数
string input = Console.ReadLine();
if (int.TryParse(input, out int n))
{
// 2. 核心数学逻辑
// C# 的 int / int 也是整除,自动舍弃小数
int units = n % 10; // 提取个位
int tens = (n / 10) % 10; // 提取十位
int hundreds = n / 100; // 提取百位

// 3. 按照逆序输出
// 使用 C# 的字符串插值($ 符号),非常直观且高效
Console.WriteLine($"{units}{tens}{hundreds}");
}
/********* End *********/
}
}

9.rust

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
use std::io; // 引入标准输入输出库

fn main() {
/********* Begin *********/
println!("input three number:");

// 1. 创建一个可变的字符串缓冲区来接收输入
let mut input = String::new();

// 2. 从标准输入读取一行
io::stdin()
.read_line(&mut input)
.expect("Failed to read line");

// 3. 将字符串修剪空格并解析为整数 (u32 表示无符号 32 位整数)
// 使用 match 处理解析失败的情况,这是 Rust 的专业写法
let n: u32 = match input.trim().parse() {
Ok(num) => num,
Err(_) => return, // 如果不是数字则退出
};

// 4. 核心数学逻辑
// Rust 的整数除法默认就是整除(同 C++/Java)
let units = n % 10; // 提取个位
let tens = (n / 10) % 10; // 提取十位
let hundreds = n / 100; // 提取百位

// 5. 格式化输出
// print! 不换行,println! 换行
print!("{}{}{}\n", units, tens, hundreds);
/********* End *********/
}

10.object c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#import <Foundation/Foundation.h>
#include <stdio.h>

int main(int argc, const char * argv[]) {
// 兼容旧版编译器的写法
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

/********* Begin *********/
printf("input three number:");
int n;
// 使用标准 C 的输入输出,避免环境干扰
if (scanf("%d", &n) == 1) {
int units = n % 10;
int tens = (n / 10) % 10;
int hundreds = n / 100;
printf("%d%d%d\n", units, tens, hundreds);
}
/********* End *********/

[pool drain];
return 0;
}

11.go

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
package main

import (
"fmt"
)

func main() {
/********* Begin *********/
// 1. 打印输入提示 (对应 std::cout << "input three number:")
fmt.Print("input three number:")

// 2. 定义变量
var n int

// 3. 读取输入 (Scanln 会等待用户输入并按下回车)
// 使用 &n 获取变量的地址,以便 Scanln 可以直接修改它的值
_, err := fmt.Scanln(&n)

if err == nil {
// 4. 核心数学逻辑
// Go 语言中整数相除会自动取整(同 C++)
units := n % 10 // 提取个位
tens := (n / 10) % 10 // 提取十位
hundreds := n / 100 // 提取百位

// 5. 按照逆序输出
// Printf 可以按照指定的格式输出,%d 表示整数
fmt.Printf("%d%d%d\n", units, tens, hundreds)
}
/********* End *********/
}

12.php

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
<?php
/********* Begin *********/
// 1. 打印输入提示 (对应 std::cout << "input three number:")
echo "input three number:";

// 2. 从标准输入读取一行数据
$input = fgets(STDIN);

// 3. 将输入转换为整数
$n = intval(trim($input));

if ($n > 0) {
// 4. 核心数学逻辑
// 注意:PHP 的 / 运算符在除不尽时会产生浮点数 (float)
// 所以必须使用 (int) 强制转换或使用 intdiv() 函数实现整除
$units = $n % 10; // 提取个位
$tens = (int)($n / 10) % 10; // 提取十位 (先除以10并取整,再取余)
$hundreds = (int)($n / 100); // 提取百位

// 5. 按照逆序输出
// PHP 中可以使用 . 来连接字符串,或者直接在双引号内放变量
echo $units . $tens . $hundreds . "\n";
}
/********* End *********/
?>

13.Visual Basic

  1. VB.NET
basic
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
Imports System

Module Module1
Sub Main()
' 1. 打印输入提示
Console.Write("input three number:")

' 2. 读取输入
Dim input As String = Console.ReadLine()
Dim n As Integer

' 3. 尝试解析输入(VB.NET 处理错误非常优雅)
If Integer.TryParse(input, n) Then

' 4. 核心数学逻辑
' 注意:VB 的 / 是浮点除法,\ 才是整数除法
Dim units As Integer = n Mod 10
Dim tens As Integer = (n \ 10) Mod 10
Dim hundreds As Integer = n \ 100

' 5. 输出结果 (无需空格,直接拼接)
Console.WriteLine("{0}{1}{2}", units, tens, hundreds)
End If

' 防止窗口直接关闭
' Console.ReadKey()
End Sub
End Module

excel版VBA

basic
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Sub ReverseNumber()
' 1. 定义变量
Dim n As Long
Dim units As Integer, tens As Integer, hundreds As Integer

' 2. 从 Excel 单元格读取数据 (假设在第一行第一列)
n = Cells(1, 1).Value

' 3. 核心数学逻辑
' 注意:VBA 和 VB 一样,整除使用 \,取余使用 Mod
units = n Mod 10
tens = (n \ 10) Mod 10
hundreds = n \ 100

' 4. 将结果写回单元格 (第一行第二列)
' 我们用 & 连接符把它们拼成字符串,防止 321 变成数学加法
Cells(1, 2).Value = units & tens & hundreds

' 弹出提示框告知用户
MsgBox "逆序完成!"
End Sub

3.windows版vbscript

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
' 1. 定义变量
Dim n, result

' 2. 弹窗获取输入
n = InputBox("请输入一个三位数:", "三位数逆序器")

' 3. 核心逻辑 (VBS 中除法默认是浮点,需用 \ 整除)
If IsNumeric(n) Then
n = CInt(n)
units = n Mod 10
tens = (n \ 10) Mod 10
hundreds = n \ 100

result = units & tens & hundreds

' 4. 弹窗输出
MsgBox "逆序结果是: " & result, vbInformation, "完成"
End If

14.lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
--[******** Begin ********]--
-- 1. 打印输入提示
io.write("input three number:")

-- 2. 读取输入并转换为数字
local input = io.read()
local n = tonumber(input)

if n then
-- 3. 核心数学逻辑
-- 注意:Lua 的 / 运算符默认执行浮点除法(类似 PHP 和 JS)
-- 在 Lua 5.3+ 中,我们可以使用 // 进行整除

local units = n % 10 -- 提取个位
local tens = math.floor(n / 10) % 10 -- 提取十位(需向下取整)
local hundreds = math.floor(n / 100) -- 提取百位

-- 4. 输出结果
-- .. 是 Lua 中的字符串连接符
print(units .. tens .. hundreds)
end
--[******** End ********]--

15.Kotlin

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.Scanner

fun main() {
// 1. 打印输入提示 (Kotlin 的标准输出非常简洁)
print("input three number:")

// 2. 使用 Scanner 获取输入 (直接复用 Java 强大的生态库)
val sc = Scanner(System.`in`)

// 3. 判断是否有整数输入
if (sc.hasNextInt()) {
val n = sc.nextInt()

// 4. 核心数学逻辑
// Kotlin 的 / 运算符在两个 Int 之间运算时,会自动执行整除 (同 C/Java)
val units = n % 10
val tens = (n / 10) % 10
val hundreds = n / 100

// 5. 按照逆序输出 (使用字符串模板,这是 Kotlin 的精华)
println("$units$tens$hundreds")
}
}

16.swift

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import Foundation

// 1. 打印输入提示
// print 默认换行,使用 terminator: "" 可以不换行
print("input three number:", terminator: "")

// 2. 读取输入并确保它不为空且是整数
if let input = readLine(), let n = Int(input) {

// 3. 核心数学逻辑
// Swift 是强类型语言,Int 之间的除法 / 自动执行整除
let units = n % 10
let tens = (n / 10) % 10
let hundreds = n / 100

// 4. 输出结果
// 使用 \(变量名) 进行字符串插值,这是 Swift 的特色
print("\(units)\(tens)\(hundreds)")
}

17.Dart

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
import 'dart:io';

void main() {
// 1. 打印输入提示
stdout.write("input three number:");

// 2. 读取输入
String? input = stdin.readLineSync();

if (input != null) {
// 3. 将字符串转为整数
int? n = int.tryParse(input);

if (n != null) {
// 4. 核心数学逻辑
// Dart 的 ~/ 运算符是专门的“整除”运算符,非常直观!
int units = n % 10;
int tens = (n ~/ 10) % 10;
int hundreds = n ~/ 100;

// 5. 输出结果 (使用 $ 字符串插值)
print("$units$tens$hundreds");
}
}
}

18.Flow Control

1
2
3
4
5
6
7
8
9
graph TD
A([开始]) --> B[/输入三位数 n/]
B --> C{是否为有效数字?}
C -- 否 --> D[/报错并退出/]
C -- 是 --> E[提取个位: units = n % 10]
E --> F[提取十位: tens = n / 10 % 10]
F --> G[提取百位: hundreds = n / 100]
G --> H[拼接输出: units, tens, hundreds]
H --> I([结束])

Reverse Numbe Question(Any Length)

1.Pseudocode

通用逆序算法 (数学逻辑版)

这个版本通过不断地“剥离”最后一位并“拼装”到新数字中,解决了位数限制问题。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
ALGORITHM ReverseAnyNumber
INPUT: Integer n
BEGIN
IF n == 0 THEN
OUTPUT 0
RETURN
END IF

reversedN = 0

// 当 n 还大于 0 时,循环继续
WHILE n > 0 DO
digit = n MOD 10 // 取出当前最后一位
reversedN = (reversedN * 10) + digit // 将原有的值进位并加上新数字
n = n / 10 // 丢弃已处理的最后一位(整除)
END WHILE

OUTPUT reversedN
END

“字符串流”伪代码版 (适合前端处理)

如果希望保留前导零(比如 100 逆序变成 001),在伪代码中会使用字符串拼接:

1
2
3
4
5
6
7
8
9
10
11
12
ALGORITHM ReverseAsText
INPUT: Integer n
BEGIN
strN = CONVERT n TO STRING
resultStr = ""

FOR i FROM (LENGTH of strN - 1) DOWN TO 0 DO
resultStr = resultStr + CHARACTER at i IN strN
END FOR

OUTPUT resultStr
END

规范的伪代码版本

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
ALGORITHM SecureReverseInteger
INPUT: n (An integer)
OUTPUT: Reversed integer or Error
BEGIN
// 1. 前置验证 (Validation)
IF n is NULL THEN RETURN ERROR

// 2. 处理特殊值 0
IF n == 0 THEN RETURN 0

// 3. 处理负数逻辑 (Sign Handling)
isNegative <- FALSE
IF n < 0 THEN
isNegative <- TRUE
n <- ABS(n) // 取绝对值,转为正数处理
END IF

reversedResult <- 0

// 4. 核心转换逻辑
WHILE n > 0 DO
digit <- n MOD 10
reversedResult <- (reversedResult * 10) + digit
n <- n / 10
END WHILE

// 5. 符号还原
IF isNegative == TRUE THEN
reversedResult <- reversedResult * -1
END IF

RETURN reversedResult
END

2.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

int main() {
long long n;
std::cout << "Enter a number: ";
std::cin >> n;

if (n == 0) {
std::cout << 0;
return 0;
}

// 核心循环:只要 n 不为 0 就不停剥离末位
while (n > 0) {
int digit = n % 10;
std::cout << digit; // 直接输出,无需存储
n /= 10;
}

return 0;
}

3.Flow Control

1
2
3
4
5
6
7
8
9
graph TD
A([开始]) --> B[result = 0]
B --> C{n > 0?}
C -- 是 --> D[digit = n % 10]
D --> E[result = result * 10 + digit]
E --> F[n = n / 10]
F --> C
C -- 否 --> G[/输出 result/]
G --> H([结束])

4.Literate Programming

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
/**
* PROBLEM: Reverse an integer of any length (e.g., 12345 -> 54321)
* STRATEGY: Iterative digit extraction and accumulation
*/

// Initialize the source number
long long originalNumber = 123456789;

// This will store our final reversed result
long long reversedResult = 0;

/* * START THE EXTRACTION LOOP
* We don't know the length, so we loop until the number is exhausted (reaches 0)
*/
while (originalNumber > 0) {

// STEP 1: Extract the "Tail"
// Use modulo 10 to grab the rightmost digit
// Example: 123 % 10 = 3
int currentDigit = originalNumber % 10;

// STEP 2: Shift and Append
// Multiply the current result by 10 to shift digits to the left (make a space)
// Then add the extracted digit into that new space
// Example: (4 * 10) + 3 = 43
reversedResult = (reversedResult * 10) + currentDigit;

// STEP 3: Truncate the Source
// Use integer division to "chop off" the digit we just processed
// Example: 123 / 10 = 12
originalNumber = originalNumber / 10;

/* * REPEAT: The loop continues: 123 -> 12 -> 1 -> 0 (Stop)
*/
}

// FINAL OUTPUT: reversedResult now holds the full mirrored value