题解 | 密码游戏

# 接收四位数输入  
n = int(input())  

# 将整数转化为字符串以便逐位操作  
n_str = str(n)  

# 保证输入是四位数  
if len(n_str) != 4:  
    raise ValueError()  

# 定义一个列表来存储每位处理后的数字  
new_digits = []  

# 对每位数字进行处理  
for digit in n_str:  
    new_digit = (int(digit) + 3) % 9  
    new_digits.append(new_digit)  

# 交换第1位和第3位,第2位和第4位  
new_digits[0], new_digits[2] = new_digits[2], new_digits[0]  
new_digits[1], new_digits[3] = new_digits[3], new_digits[1]  

# 将数字重新转化为字符串,确保是四位数格式  
result = ''.join(str(digit) for digit in new_digits)  

print(result)

全部评论

相关推荐

不愿透露姓名的神秘牛友
01-31 04:00
神哥不得了:首先我就是在成都,成都的互联网格外的卷,如果是凭现在的简历的话很难找到大厂,建议再添加一个高质量的项目上去,另外专业技能的话最好是超过每一条的一半
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务