输入一个四位数,将其加密后输出。方法是将该数每一位上的数字加9,然后除以10取余,做为该位上的新数字,最后将千位和十位上的数字互换,百位和个位上的数字互换,组成加密后的新四位数。例如输入1257,经过加9取余后得到新数字0146,再经过两次换位后得到4601
时间: 2024-05-03 13:17:27 浏览: 21
好的,以下是 Python 代码实现:
```python
num = input("请输入一个四位数:")
# 将字符串转换为整数
num = int(num)
# 获取千位、百位、十位和个位上的数字
thousand = num // 1000
hundred = num % 1000 // 100
ten = num % 100 // 10
one = num % 10
# 对每一位上的数字加9,然后除以10取余得到新数字
new_thousand = (thousand + 9) % 10
new_hundred = (hundred + 9) % 10
new_ten = (ten + 9) % 10
new_one = (one + 9) % 10
# 交换千位和十位上的数字,百位和个位上的数字
temp = new_thousand
new_thousand = new_ten
new_ten = temp
temp = new_hundred
new_hundred = new_one
new_one = temp
# 组成加密后的新四位数
new_num = new_thousand * 1000 + new_hundred * 100 + new_ten * 10 + new_one
print("加密后的数字为:", new_num)
```
运行示例:
```
请输入一个四位数:1257
加密后的数字为: 4601
```
相关问题
c语言代码,输入一个四位数,将其加密后输出。方法是将该数每一位上的数字加9,然后除以10取余,做为该位上的新数字,最后将千位和十位上的数字互换,百位和个位上的数字互换,组成加密后的新四位数。例如输入1
C语言代码如下:
```c
#include <stdio.h>
int main() {
int num, encryptedNum, temp;
printf("请输入一个四位数:");
scanf("%d", &num);
// 加密操作
num = (num + 9999) % 10000; // 防止输入的数大于9999
// 每一位上的数字加9并取余
num = ((num / 1000 + 9) % 10) * 1000 + ((num / 100 % 10 + 9) % 10) * 100 + ((num / 10 % 10 + 9) % 10) * 10 + ((num % 10 + 9) % 10);
// 交换千位和十位上的数字
temp = num / 1000;
num = num - temp * 1000;
num = num + temp * 10;
// 交换百位和个位上的数字
temp = num / 100;
num = num - temp * 100;
num = num + temp * 1000;
printf("加密后的数字为:%d\n", num);
return 0;
}
```
输入一个四位数,将其加密后输出。方法是将该数每一位上的数字加9,然后除以10取余,作为该位上的新数字,最后将千位和十位上的数字互换,百位和个位上的数字互换,组成加密后的新四位数。用Python
实现如下:
```python
num = int(input("请输入一个四位数:"))
# 分离每一位数字
thousands = num // 1000
hundreds = num // 100 % 10
tens = num // 10 % 10
ones = num % 10
# 加密每一位数字
thousands = (thousands + 9) % 10
hundreds = (hundreds + 9) % 10
tens = (tens + 9) % 10
ones = (ones + 9) % 10
# 交换千位和十位,百位和个位
temp = thousands
thousands = tens
tens = temp
temp = hundreds
hundreds = ones
ones = temp
# 组成加密后的新四位数
new_num = thousands * 1000 + hundreds * 100 + tens * 10 + ones
print("加密后的数字为:", new_num)
```
输出示例:
```
请输入一个四位数:1234
加密后的数字为: 0189
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)