三位数倒着读编程怎么写

时间:2025-01-29 03:18:37 网络游戏

C语言

方法一

```c

include

int main() {

int n, a, b, c;

printf("请输入一个三位数:");

scanf("%d", &n);

a = n / 100;

b = (n / 10) % 10;

c = n % 10;

int m = c * 100 + b * 10 + a;

printf("倒序输出该三位数:%d%d%d\n", m);

return 0;

}

```

方法二

```c

include

int main() {

int num, a, b, c;

printf("请输入一个三位数:\n");

scanf("%d", &num);

a = num / 100;

b = num % 100 / 10;

c = num % 10;

int figure = c * 100 + b * 10 + a;

printf("倒序输出的三位数是:\n");

printf("%d\n", figure);

return 0;

}

```

方法三

```c

include

int main() {

int h, i, j;

printf("请输入一个三位数: ");

scanf("%1d%1d%1d", &h, &i, &j); // %1d 表示每次只读取一位数字

printf("倒序输出该三位数 %d%d%d\n", j, h, i);

return 0;

}

```

Python

```python

def reverse_three_digit_number(num):

if 100 <= num <= 999:

reversed_num = str(num)[::-1]

return int(reversed_num)

else:

return "请输入一个三位数"

user_input = int(input("请输入一个三位数: "))

result = reverse_three_digit_number(user_input)

print(f"反向输出的结果是: {result}")

```

Java

```java

import java.util.Scanner;

public class ReverseThreeDigitNumber {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("请输入一个三位数:");

int num = scanner.nextInt();

int reversedNum = reverse(num);

System.out.println("倒序输出的三位数是: " + reversedNum);

}

public static int reverse(int num) {

int a = num / 100;

int b = (num % 100) / 10;

int c = num % 10;

return c * 100 + b * 10 + a;

}

}

```

C++

```cpp

include

include

include

int main() {

int num;

std::cout << "请输入一个三位数: ";

std::cin >> num;

std::string numStr = std::to_string(num);

std::reverse(numStr.begin(), numStr.end());

int reversedNum = std::stoi(numStr);

std::cout << "倒序输出的三位数是: " << reversedNum << std::endl;

return 0;

}

```

这些代码示例展示了如何在不同的编程语言中实现三位数的倒序输出。你可以根据自己的需求和编程环境选择合适的方法。