实战一道2级综合编程模拟题

Posted jamesxu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实战一道2级综合编程模拟题相关的知识,希望对你有一定的参考价值。

请编写程序,生成随机密码。具体要求如下:????????????????????????????????????????????????????????????????????????????????????????????????

(1)使用 random 库,采用 0x1010 作为随机数种子。????????????????????????????????????????????????????????????????????????????????????????????????

(2)密码 [email protected]#$%^&* 中的字符组成。????????????????????????????????????????????????????????????????????????????????????????????????

(3)每个密码长度固定为 10 个字符。????????????????????????????????????????????????????????????????????????????????????????????????

(4)程序运行每次产生 10 个密码,每个密码一行。????????????????????????????????????????????????????????????????????????????????????????????????

(5)每次产生的 10 个密码首字符不能一样。????????????????????????????????????????????????????????????????????????????????????????????????

(6)程序运行后产生的密码保存在“随机密码.txt”文件中。????????????????????????????????????????????????????????????????????????????????????????????????

 ????????????????????????????????????????????????????????????????????????????????????????????????

不写入文件,直接 print 10 个密码即可自动评阅

 

解析:

通读题目要求,req5是个难点,如何保证产生的10个密码首字符不同?
一个自然的想法是记录并比较
进而对于req4,就不能用for循环固定循环次数,需要用while循环
while循环退出循环的依据是什么?取决于如何记录密码,比如把每次生成的密码记录到一个列表,用len(ls)<10做退出循环的条件
每次生成的密码设为cipher,考虑以及几点
1.如何生成2.如何满足req5.3如何输出
1.如何生成,可以考虑用s[random.randint(0,len(s)-1)]或random.choice(s), s 是字符集
2.如何满足req5,可以记录每个cipher的首字符,并记录在字符串execlude,只有满足的才添加到ls中
3. 输出,每个密码一行,cipher+ ‘\n‘就可以了

具体代码如下:

import random

fo = open(D:/bak/skydrive/automation/2级/execise/随机密码.txt,w)
random.seed(0x1010)
s = [email protected]#$%^&*
cipher = ‘‘ # store a cipher,store it into ls and then clear it
ls = [] # store all 10 cipher, len(ls) to count the num of cipher
execlude = ‘‘ # store the first char of a cipher,make sure the cipher meet req#5
while len(ls) < 10: # generate 10 cipher, not use for loop as due to req#5, the running time is not predictable
    cipher = ‘‘ # reset cipher each round
    for i in range(10): # 10个字符
        #cipher += random.choice(s) # pickup a random char from s by random.choice and then connect cyper by +
        cipher += s[random.randint(0,len(s)-1)]        
    cipher = cipher + \n
    if cipher[0] in execlude:
        continue
    else:
        ls.append(cipher)
        execlude += cipher[0]
        #fo.writelines(cipher) # if write line by line,the performance is somewhat poor,let write the ls to file directly
#print(c)
print(‘‘.join(ls))
##for line in ls:
##    fo.write(line)
fo.write(‘‘.join(ls))
#print(execlude)
fo.close()

    

执行结果:

So2WpkoC7i
armJ86eUG9
B*GcqsYC^B
wQ3bcfcAJy
Xdyg8pQTIS
YO!1YH1AP3
[email protected]&
[email protected]$TBfp
TBm#WfYNHr
Ue75y$E9Cv

由此,可见2级综合编程题,需要在较短时间内,能解读问题,综合应用各种基本编程技能,完成题目要求,对初学者还是有一定难度的。

需要仔细审题,吃透题意,提高基本数据处理的熟练度,才能从容过关。

 

以上是关于实战一道2级综合编程模拟题的主要内容,如果未能解决你的问题,请参考以下文章

编程训练-考研上机模拟综合模拟2-2019浙大上机模拟(晴神)

装甲战斗车辆类综合仿真模拟训练系统软件

编程训练-考研上机模拟综合模拟1-2019浙大上机模拟(晴神)

verilog hdl中啥是综合?啥是模拟?

Node.js-Koa2框架生态实战-从零模拟新浪微博 完整教程

2017级算法模拟上机准备篇(序列DP 进阶_1)