输入一个四位数,将其加密后输出。方法是将该数每一位上的数字加9,然后除以10取余,作为该位上的新数字,最后将千位和十位上的数字互换,百位和个位上的数字互换,组成加密后的新四位数。用Python
时间: 2024-04-29 10:25:24 浏览: 25
实现如下:
```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
```
相关问题
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;
}
```
输入1个四位数,将其加密后输出。方法是将该数每一位上的数字加9,然后除以10取余,做为该位上的新数字,最后将第1位和第3位上的数字互换,第2位和第4位上的数字互换,组成加密后的新数。
题目:输入1个四位数,将其加密后输出。方法是将该数每一位上的数字加9,然后除以10取余,做为该位上的新数字,最后将第1位和第3位上的数字互换,第2位和第4位上的数字互换,组成加密后的新数字。
解法:先将输入的四位数拆分成单个数字,然后按要求加密。具体实现方法是,将每一位数字加上9后再取余10,得到新的数字,然后将第1位和第3位上的数字互换,第2位和第4位上的数字互换,组成加密后的新数字,最后输出即可。
相关推荐
![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)
![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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)