当前位置:网站首页>【每周一坑】信息加密 +【解答】正整数分解质因数
【每周一坑】信息加密 +【解答】正整数分解质因数
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

边栏推荐
- 永磁同步电机转子位置估算专题 —— 基波模型与转子位置角
- In unity space, an object moves around a fixed point on the sphere at a fixed speed
- Leetcode question 448 Find all missing numbers in the array
- Redisson bug analysis
- 永磁同步电机转子位置估算专题 —— 基波模型类位置估算概要
- Monthly report of speech synthesis (TTS) and speech recognition (ASR) papers in June 2022
- String长度限制?
- 句号压缩过滤器
- 系统与应用监控的思路和方法
- Poj1149 pigs [maximum flow]
猜你喜欢

5. 无线体内纳米网:十大“可行吗?”问题

Node. Js: express + MySQL realizes registration, login and identity authentication

Notes on beagleboneblack

Ideas and methods of system and application monitoring

Continuous test (CT) practical experience sharing

Jupyter launch didn't respond after Anaconda was installed & the web page was opened and ran without execution

Case ① | host security construction: best practice of 3 levels and 11 capabilities

Discussion on beegfs high availability mode

持续测试(CT)实战经验分享
![[cloud native and 5g] micro services support 5g core network](/img/c9/4ccacd1e70285c2ceb50c324e5018c.png)
[cloud native and 5g] micro services support 5g core network
随机推荐
Speech recognition (ASR) paper selection: talcs: an open source Mandarin English code switching corps and a speech
Wechat applet common collection
2022年6月语音合成(TTS)和语音识别(ASR)论文月报
爬虫(14) - Scrapy-Redis分布式爬虫(1) | 详解
Redisson bug analysis
Example of shutter text component
颜色(color)转换为三刺激值(r/g/b)(干股)
案例 ①|主机安全建设:3个层级,11大能力的最佳实践
[Yann Lecun likes the red stone neural network made by minecraft]
Unity writes a timer tool to start timing from the whole point. The format is: 00:00:00
腾讯架构师首发,2022Android面试笔试总结
Poj3617 best cow line
报错分析~csdn反弹shell报错
永磁同步电机转子位置估算专题 —— 基波模型与转子位置角
枚举根据参数获取值
[network planning] Chapter 3 data link layer (4) LAN, Ethernet, WLAN, VLAN
【云原生与5G】微服务加持5G核心网
(3) Web security | penetration testing | basic knowledge of network security construction, IIS website construction, EXE backdoor generation tool quasar, basic use of
22-07-05 七牛云存储图片、用户头像上传
[cloud lesson] EI lesson 47 Mrs offline data analysis - processing OBS data through Flink