发布网友 发布时间:2022-05-13 21:00
共2个回答
热心网友 时间:2023-10-28 04:58
具体代码如下,已测试可以正常运行
import random
def func(m, n):
random.seed(50)
result = {}
least_count_num = {'num': 0, 'count':1000}
most_count_num = {'num': 0, 'count':0}
for i in range(1000):
x = random.randint(m, n)
count = result.get(str(x), 0)
result[str(x)] = count + 1
for k, v in result.items():
if v > most_count_num["count"]:
most_count_num["num"] = int(k)
most_count_num["count"] = v
if v < least_count_num["count"]:
least_count_num["num"] = int(k)
least_count_num["count"] = v
print('出现最多的数字是%d出现了%d次,出现最少数字是%d,出现了%d次' % (most_count_num["num"], most_count_num["count"], least_count_num["num"], least_count_num["count"]))
输出样例要这样怎么办
Exited with error status 1
Traceback (most recent call last):
File "de0d88cad174d57903fc91df023ce78b", line 19, in <mole>
print('出现最多的数字是%d,出现了%d次,出现最少数字是%d,出现了%d次'%(most_count_num["num"], most_count_num["count"], least_count_num["num"], least_count_num["count"]))
NameError: name 'most_count_num' is not defined
为什么有这个
热心网友 时间:2023-10-28 04:58
代码:
import random
random.seed(50)
m,n=int(input()),int(input())
l=[random.randint(m,n) for i in range(1000)]
l=[(e,l.count(e)) for e in set(l)]
l.sort(key=lambda t:t[1])
print('{}:{}'.format(*l[-1]))
print('{}:{}'.format(*l[0]))
运行结果截图: