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

循环练习

时间:2018-07-28 16:43:40      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:int   吉利   var   size   [1]   关于   while   空格   地方   

# 双层while 写 10行10列的@

i = 0
while i < 10:
  j = 0
  while j<10:
    print("@",end="")
    j+=1
  print()
  i+=1



# 双层 while 写 10行10列的隔行,(或隔列) 换色的●○
i = 0
while i <10:
  j = 0
  while j<10:
    if i % 2 == 0:
      print("●",end="")
    else:
      print("○",end="")
    j+=1
  print()
  i+=1


# ( 或隔列 )
i = 0
while i <10:
  j = 0
  while j<10:
    if j % 2 == 0:
      print("●",end="")
    else:
      print("○",end="")
    j+=1
  print()
  i+=1

# 关于字符串的格式化 %d %f %s
# %d 用来表示一个占位符 用来替换整形
# %f 用来表示一个占位符 用来替换浮点形
# %s 用来表示一个占位符 用来替换字符串
# res = 123
"""
%2d 是%d 占了2位 默认右对齐
如果加了- 默认左对齐
"""

"""

res = "%d"%(123)
res = "%d%d%d"%(12,13,14)
res = "%d"%(3.99) # 小数点舍弃
res = "%-3d"%(8)

print(res)
res2 = "%3d"%(12)
print(res2)

# %f 存在小数进制问题
res = "%f"%(3)
res = "%.1f"%(3)
print(res)

res = "%s"%("撒地方")
print(res)
res = "%s"%("today weather good good o")
print(res)


res = "---%d-----%s----%.1f"%(345,"沙发",3.999)
print(res)
"""

# 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
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

print("<===>")
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

print("<====>")
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


# 吉利数字: 666 888 999 123 321 100 ~ 999 找出来
#257 n // 10 % 10


# 方法一
i = 100
while i<=999:
  gewei = i % 10
  shiwei = i // 10 % 10
  baiwei = i // 10 // 10

  if (shiwei == gewei) and (shiwei == baiwei) :
    print(i)

  if (shiwei == gewei + 1) and (shiwei == baiwei - 1):
    print(i)

  if (shiwei == gewei - 1) and (shiwei == baiwei + 1):
    print(i)

  i+=1


#方法二
print("<====>")
i = 100
while i<=999:
  strvar = str(i)
  baiwei = int(strvar[0])
  shiwei = int(strvar[1])
  gewei = int(strvar[2])

  if (shiwei == gewei) and (shiwei == baiwei):
    print(i)

  if (shiwei == gewei + 1) and (shiwei == baiwei - 1):
    print(i)

  if (shiwei == gewei - 1) and (shiwei == baiwei + 1):
    print(i)

  i+=1

"""
百钱买白鸡
百钱白鸡: 公鸡 = 1 母鸡 = 3 小鸡 = 0.5 100块钱 买 100只鸡 可以有多少种买法
"""

"""
x+y+z = 100
x + 3y + 0.5z = 100
"""

# x,y,z 分别代表公鸡,母鸡,小鸡 公鸡最多买100只鸡,母鸡最大33只,小鸡 最多100鸡
x = 0
while x<=100:
  y=0
  while y<=33:
    z=0
    while z<=100:
      if (x+y+z == 100) and (x+3*y+0.5*z == 100):
        print(x,y,z)
      z+=1
    y+=1
  x+=1

循环练习

标签:int   吉利   var   size   [1]   关于   while   空格   地方   

原文地址:https://www.cnblogs.com/bling-bling-star/p/9382385.html

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