python基础语法记录

python基础语法以及基础编程记录:

  1. 隔列打印星星:
#单个循环打印星星:
i = 0
while i < 100:
#此处执行打印星星的命令:
	if i % 2 == 0:
		print("☆", end = " ")
	else:
		print("★", end = " ")
#此处执行换行的命令:
	if i % 10 == 9:
		print()
	i += 1


#双循环打印小星星偷懒之一:
j = 0
while j < 5:
	i = 0
	while i < 5:
		print("♡♥", end="")
		i += 1
	print()
	j += 1

#双循环打印小星星之二:

  1. 隔行打印星星:
#我刚开始想的单循环打印隔行小星星:
i = 0
j = 0
while i < 100:
	if i // 10 == j and j % 2 == 0:
		print("★", end = "")
	else:
		print("☆", end = "")
	if i % 10 == 9:
		print()
	i += 1
	if i % 10 == 0:
		j += 1
		
#稍微优化了一下的代码写出的隔行小星星: 
i = 0
while i < 100:
#隔行打印星星
	if i // 10 % 2 == 0:
		print("★", end = "")
	else:
		print("☆", end = "")
#换行:
	if i % 10 == 9:
		print()
	i += 1

#双重循环的隔行小星星:
j = 0
while j < 10:
	if j % 2 == 0:
		i = 0
		while i < 10:
				print("♡", end="")
				i += 1
	else:
		i = 0
		while i < 10:
				print("♥", end="")
				i += 1
	print()
	j += 1

#稍微聪明一点点的代码:
j = 0
while j < 10:
	i = 0
	while i < 10:
		if j % 2 == 0:
			print("♡", end = "")
		else:
			print("♥", end = "")
		i += 1
	print()
	j += 1
  1. 打印99乘法表:
# 99乘法表:
# 1. 正序:

i = 1
while i < 10:
	j = 1
	while j <= i:
		print("%d * %d = %-2d" % (i, j, i*j), end = " ")
		j += 1
	print()
	i += 1

# for 循环
for i in range(1, 10):
	for j in range(1, i+1):
		print("%d * %d = %-2d" % (i, j, i * j), end = " ")
	print()

# 2. 倒序:

i = 1
while i < 10:
	j = i
	while j < 10:
		print("%d * %d = %-2d" % (i, j, i * j), end=" ")
		j += 1
	print()
	i += 1

#for 循环:

for i in range(1, 10):
	for j in range(i, 10):
		print("%d * %d = %-2d" % (i, j, i * j), end = " ")
	print()

# 2. 空格推正序:

i = 1
while i < 10:
	j = i
	while j < 9:
		print(end = "           ")
		j += 1
	j = 1
	while j <= i:
		print("%d * %d = %-2d" % (i, j, i * j), end = " ")
		j += 1
	print()
	i += 1

# 空格推倒序:

i = 1
while i < 10:
	j = 10 - i
	while j < 9:
		print(end = "           ")
		j += 1
	j = i
	while j < 10:
		print("%d * %d = %-2d" % (i, j, i * j), end = " ")
		j += 1
	print()
	i += 1

效果如下:
在这里插入图片描述
在这里插入图片描述

  1. 打印吉利数字 形如111 222 333 123 321 234 456 654 432 这种的数字 重复或者升序或者降序:

#没错  三个循环  时间复杂度最高   俺写的(╥╯^╰╥)
i = 1
while i < 10:
	j = 0
	while j < 10:
		k = 0
		while k < 10:
			if i == j and j == k:
				print("%d%d%d" % (i, j, k), end = "  ")
			if i - j == 1 and j - k == 1:
				print("%d%d%d" % (i, j, k), end = "  ")
			if k -j == 1 and j - i == 1:
				print("%d%d%d" % (i, j, k), end = "  ")
			k += 1
		j += 1
	print()
	i += 1

#参考比我厉害的大佬写的, 利用地板除, 取余, 分别把个位, 十位, 百位取出来
i = 100
while i < 1000:
	gewei = i % 10
	shiwei = i // 10 % 10
	baiwei = i // 100
	if gewei == shiwei and shiwei == baiwei:
		print(i, end = " ")
	if shiwei - gewei == 1 and baiwei - shiwei == 1:
		print(i, end = " ")
	if gewei - shiwei == 1 and shiwei - baiwei == 1:
		print(i, end = " ")
	i += 1
#另外一种版本, 先转换成字符串, 然后在强转回来, 进行比较(大佬还是牛逼的):
i = 100
while i < 1000:
	num = str(i)
	gewei = int(num[-1])
	shiwei = int(num[-2])
	baiwei = int(num[0])   '强转'
	if gewei == shiwei and baiwei == shiwei:
		print(i, end = " ")
	if gewei - shiwei == 1 and shiwei - baiwei == 1:
		print(i, end=" ")
	if shiwei - gewei == 1 and baiwei - shiwei  == 1:
		print(i, end=" ")
	i += 1
  1. 百钱百鸡, 1块钱的公鸡, 3块钱的母鸡, 5毛钱的小鸡仔, 刚好100块买100只鸡:
#干啥啥不行   穷举第一名
#没有什么是我穷举解决不了的, 如果有, 那一定是时间复杂度
i = 0
cnt = 0
while i <= 100:
	j = 0
	while j < 34 :
		k = 100 - i - j
		if i + 3 * j + 0.5 * k == 100.0:
			cnt += 1
		j += 1
	i += 1
print(cnt)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值