当前位置:网站首页>【每周一坑】信息加密 +【解答】正整数分解质因数
【每周一坑】信息加密 +【解答】正整数分解质因数
2022-07-06 12:23:00 【Crossin的编程教室】
零基础python入门教程:python666.cn
大家好,欢迎来到 Crossin的编程教室 !
今天给大家来一个我比较喜欢的话题之一:信息加密
当然,这道题算是信息加密的皮毛的皮毛了。
请听题:
阿圆和奇奇在上课的时候传递小抄,但又不想小抄的内容被其他人发现。于是他俩就约定了一种加密方式:
只对信息内容中的英语或者拼音(也就是字母)进行加密,其他符号保留原样
每个字母向后移动5位,成为密文,比如 a->f,k->p
如果向后5位超过了z,就折回字母开头,比如 x->c
请用一段代码完成此规则的加密和解密。
参考示例:
【加密】
输入:there is a vegetable in your teeth
输出:ymjwj nx f ajljyfgqj ns dtzw yjjym
【解密】
输入:onz sn mzf izt
输出:jiu ni hua duo
附加题
你能否解密出这段文字:
ild gdpsh sxktgvts xc p ldds, pcs x--
x iddz iwt dct athh igpktats qn,
pcs iwpi wph bpst paa iwt sxuutgtcrt.详细解答和参考代码将在下期栏目中给出,也可以参考其他同学在留言中的代码。
期待各位同学提交解答,更期待你能完成整个系列。
简单代码可直接在留言中提交,较长代码推荐使用 paste.ubuntu.com 或
codeshare.io 等代码分享网站,只需将代码复制上去保存,即可获得一个分享地址,非常方便。
往期问题可点击文章开头的合集“每周一坑”进入查看。
【解答】正整数分解质因数
本题的一种思路是:
从2开始尝试去整除原数N
如果可以整除,原数更新为 N//i,并输出 i;不能整除就继续试,直到N被自身整除
如果之前已经有质因数,最后剩下的 i 就是最后一个质因数;如果没有,说明原数就是质数
这里之所以不用判断除数 i 是不是质数,是因为 i 是从 2 开始逐个试过来的,如果它可以被更小的数整除,那么之前就已经被处理过了。因此能够被计算到的 i 一定就是质因数。
另外这里有个可以小小优化的点:每次尝试不一定要从2开始,而可以从上一个质因数开始。
参考代码:
n = input('输入待分解的正整数:')
start = 2
is_prime = True
while n > 1: # 不断分解n,直到分解为1
for index in range(start, n + 1) :
if n % index == 0: # n能被index整除
n //= index # n=n//index
if n == 1:
if is_prime:
print(f'{index}是质数')
else:
print(index)
else: # index 一定是素数
print(f'{index} * ', end='')
is_prime = False
break
start = index_往期文章推荐_
如需了解付费精品课程及教学答疑服务
请在Crossin的编程教室内回复: 666

边栏推荐
- Technology sharing | packet capturing analysis TCP protocol
- Wonderful coding [hexadecimal conversion]
- Cesium 点击绘制圆形(动态绘制圆形)
- HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother
- OceanBase社区版之OBD方式部署方式单机安装
- 【GET-4】
- Wechat applet common collection
- JS implementation force deduction 71 question simplified path
- 腾讯架构师首发,2022Android面试笔试总结
- Maximum likelihood estimation and cross entropy loss
猜你喜欢

Redisson bug analysis
Tencent T4 architect, Android interview Foundation

【计网】第三章 数据链路层(3)信道划分介质访问控制
腾讯T2大牛亲自讲解,跳槽薪资翻倍

Pay attention to the partners on the recruitment website of fishing! The monitoring system may have set you as "high risk of leaving"

Enumeration gets values based on parameters

Notes on beagleboneblack

永磁同步电机转子位置估算专题 —— 基波模型与转子位置角

redisson bug分析

Case ① | host security construction: best practice of 3 levels and 11 capabilities
随机推荐
转让malloc()该功能后,发生了什么事内核?附malloc()和free()实现源
腾讯T3大牛手把手教你,大厂内部资料
Initial experience of addresssanitizer Technology
Case ① | host security construction: best practice of 3 levels and 11 capabilities
Pay attention to the partners on the recruitment website of fishing! The monitoring system may have set you as "high risk of leaving"
Tencent byte Alibaba Xiaomi jd.com offer got a soft hand, and the teacher said it was great
Node. Js: express + MySQL realizes registration, login and identity authentication
Learn to punch in Web
Linear distance between two points of cesium
小微企业难做账?智能代账小工具快用起来
爬虫(14) - Scrapy-Redis分布式爬虫(1) | 详解
mod_wsgi + pymssql通路SQL Server座
【云原生与5G】微服务加持5G核心网
Node.js: express + MySQL实现注册登录,身份认证
深度学习分类网络 -- ZFNet
Ideas and methods of system and application monitoring
An East SMS login resurrection installation and deployment tutorial
PHP and excel phpexcel
句号压缩过滤器
redisson bug分析