码迷,mamicode.com
首页 > 其他好文 > 详细

双层循环经典小项目题

时间:2019-04-30 23:23:09      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:方向   while   除了   一个   lse   pre   表达式   for循环   and   

# (1) 99 乘法表 方向一
i = 1
while i<=9:

	# 打印表达式
	j = 1
	while j <= i:
		print("%d*%d=%2d " % (i,j,i*j) ,end="")
		j+=1
	
	# 打印换行
	print()
	i+=1

# 方向二
i = 1
while i<=9:
	
	# 打印空格
	k = 9 - i
	while k>0:
		print("       ",end="")
		k -= 1

	# 打印乘法表达式
	j = 1
	while j<=i:
		print("%d*%d=%2d " % (i,j,i*j),end="")
		j+=1
	
	# 打印换行
	print()
	
	i+=1

"""

k = 8
while k>0:
	print("   " * k,end="")
	print(1)
	k-=1

1 => 8
2 => 7
3 => 6
4 => 5
...
8 => 1
"""

# 方向三
print("<==>")
i = 9
while i>0:

	# 打印表达式
	j = 1
	while j <= i:
		print("%d*%d=%2d " % (i,j,i*j) ,end="")
		j+=1
	
	# 打印换行
	print()
	i-=1

# 方向四
i = 9
while i>0:
	
	# 打印空格
	k = 9 - i
	while k>0:
		print("       ",end="")
		k -= 1

	# 打印乘法表达式
	j = 1
	while j<=i:
		print("%d*%d=%2d " % (i,j,i*j),end="")
		j+=1
	
	# 打印换行
	print()
	
	i-=1


# 1.100~ 999 找吉利数字  111 222 333 ... 999  123 321  456 654 ..678 876 打印出来
# 写法一
‘‘‘
789
个位: 789 % 10
十位: 789 // 10 % 10 => 78 % 10 => 8
百位: 789 // 100
‘‘‘
i = 100
while i<=999:

	# 算出个位
	gewei = i % 10
	# 算出十位
	shiwei = i // 10 % 10
	# 算出百位
	baiwei = i // 100
	
	if shiwei == gewei and shiwei == baiwei :
		print(i)
	
	# 123 678
	if shiwei == gewei-1 and shiwei == baiwei + 1:
		print(i)
	
	# 321 876
	if shiwei == gewei+1 and shiwei == baiwei - 1:
		print(i)
	
	i+=1

# 写法二
‘‘‘
strvar = "789"
strvar[0] => "7"
strvar[1] => "8"
strvar[-1] => "9"
‘‘‘
print("<==========================>")
‘‘‘
i = 100
while i<=999:
	strvar = str(i)
	# 算出个位
	gewei = int(strvar[0])
	# 算出十位
	shiwei = int(strvar[1])
	# 算出百位
	baiwei = int(strvar[-1])
	
	if shiwei == gewei and shiwei == baiwei :
		print(i)
	
	# 123 678
	if shiwei == gewei-1 and shiwei == baiwei + 1:
		print(i)
	
	# 321 876
	if shiwei == gewei+1 and shiwei == baiwei - 1:
		print(i)
	
	i+=1
‘‘‘

# 2.百钱买百鸡  公鸡1块钱一只  母鸡 3块钱一只  小鸡5毛钱一只 100块钱 买 100只鸡 有多少种买法(升级)
"""
# 穷举法: 一个一个的试
公鸡x 母鸡y 小鸡z x+y+z = 100
x 1,2
y 3,4
z 5,6
x+y+z=10 ?

x = 1
1 + 3 + 5
1 + 3 + 6  = 10
1 + 4 + 5  = 10
1 + 4 + 6


x = 2
2 + 3 + 5 = 10
2 + 3 + 6
2 + 4 + 5
2 + 4 + 6
"""
x = 0
while x <= 100:

	y = 0
	while y<=33:
	
		z = 0 
		while z<=100:
		
			if (x+y+z == 100) and  (x*1 + 3*y + 0.5 * z == 100):
				print(x,y,z)
			z+=1
	
		y+=1

	x+=1


# 7.写代码:计算 1 - 2 + 3 -4 + 5 -6 ... ... + 99 中除了88以外所有数的总和?
i = 1
total = 0
while i <= 99:

	if i == 88:
		i+=1
		continue 

	if i % 2 == 1:
		# 1 + 3 + 5+ 7 + 9...
		total += i
		# total = 1 + 3 + 5 +7 ...
	else:
		# -2 -4 -6 -8 -10.....
		total -= i
		# total = -2 -4 -6 -8
		
	i+=1

print("<===>")
print(total)
# 2500 - 2450 = 50
# 2450


# for循环自动完成加一操作
total = 0
for i in range(1,100):
	if i == 88:
		continue 

	if i % 2 == 1:
		total += i
	else:
		total -= i
print(total)

"""
9~1
for i in range(9,0,-1)
	print(i)

j = 1
while j<=i:
	pass

for i in  range(1,i+1):
	pass  # 1 ~ i
"""

  

双层循环经典小项目题

标签:方向   while   除了   一个   lse   pre   表达式   for循环   and   

原文地址:https://www.cnblogs.com/huangjiangyong/p/10798477.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!