python 蓝桥杯之枚举、尺取法

文章目录


幸运数字

  • 对于该题的思路:我们可以采用直接暴力枚举的方法
    幸运数字就是 3 i 3^i 3i5 j 5^j 5j 7 k 7^k 7k 的形式,那么我们就确定一个终极范围,直接用三层循环来遍历即可
  • 对于细节上的计数的话:相乘的结果只要小于等于规定的 num 就可以直接加一,不过最后要减1,因为最小的数 1 不符合要求

先在 IDLE 上 使用暴力得出结果,然后再在网页上提交最后的结果即可

python 复制代码
num = 59084709587505
count = 0
for i in range(50):
    for j in range(50):
        for k in range(50):
            result = 3**i * 5**j * 7**k
            if result <=num:
                count = count + 1

print(count-1)
                
#1905

组合型枚举


python 复制代码
chosen = []
n = 0
m = 0
def calc(x):
	if len(chosen)>m:
		return
	if len(chosen) + n-x+1<m:
		return
	if x== n+1:
		for i in chosen:
			print(i,end='')
		print()
		return
	chosen.append(x)

排列型枚举


python 复制代码
order = [0]*20
chosen = [0]*20
n = 0
def calc(x):
	if x==n+1:
		ansTem=''
		for i in range(1,n+1):
			print(order[i],end='')
		print()
		return
	for i in range(1,n+1):
		if(chosen[i]==1):
			continue
		order[x]=i
		chosen[i]=1
		calc(x+1)
		chosen[i]=0
		order[x]=0
	

python 排列函数

子集


python 复制代码
a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]


def print_subset(n):
    for i in range(1<<n):

        for j in range(n):
            if (i&(1<<j))!=0:
                print(a[j],end='')
        print()

n = 3

print_subset(3)

尺取法





相关推荐
zuoerjinshu4 小时前
sql实战解析-sum()over(partition by xx order by xx)
数据库·sql
NocoBase5 小时前
【2.0 教程】第 1 章:认识 NocoBase ,5 分钟跑起来
数据库·人工智能·开源·github·无代码
猿界零零七5 小时前
pip install mxnet 报错解决方案
python·pip·mxnet
Hoshino.416 小时前
基于Linux中的数据库操作——下载与安装(1)
linux·运维·数据库
不只会拍照的程序猿7 小时前
《嵌入式AI筑基笔记02:Python数据类型01,从C的“硬核”到Python的“包容”》
人工智能·笔记·python
Jay_Franklin7 小时前
Quarto与Python集成使用
开发语言·python·markdown
Oueii8 小时前
Django全栈开发入门:构建一个博客系统
jvm·数据库·python
未来龙皇小蓝8 小时前
【MySQL-索引调优】11:Group by相关概念
数据库·mysql·性能优化
2401_831824968 小时前
使用Fabric自动化你的部署流程
jvm·数据库·python
njidf8 小时前
Python日志记录(Logging)最佳实践
jvm·数据库·python