zoukankan      html  css  js  c++  java
  • 单项双项循环经典练习题

    1.打印一行十个小星星(单项循环)
    i=0
    while i<10:
      print('*',end='')
      i+=1
     
    2.通过变量打印一行十个小星星
    i=0
    strvar=''
    while i<10:
      strvar+='*'
      i+=1
    print(strvar)

    3.打印十行十列小星星

    (1)单项循环:

    i=0
    while i<100:
      print('*',end='')
      if i%10==9:
       print()
      i+=1
    (2)while双向循环
    i=0
    while i<10:
      j=0
      while j<10:
        print('*',end='')
        j+=1
      print()
      i+=1

    (3)for 双向循环
    for i in range(10):
      for j in range(10):
        print('*',end='')
      print()
    4.打印一行十个相间换色小星星
    (1)while循环
    i=0
    while i<10:
      if i%2==0:
        print('*',end='')
      else:
        print('#',end='')
      i+=1
    (2)for循环
    for i in range(10):
      if i%2==0:
        print('*',end='')
      else:
        print('#',end='')
    5.打印十行十列隔列换色小星星
    (1)单项循环:
    i=0
    while i<100:
      #打印小星星
      if i%2==0:
        print('*',end='')
      else:
        print('#',end='')
     #打印换行
      if i%10==9:
        print()
      i+=1

    (2)while双向循环
    i=0
    while i<10:
      j=0
      while j<10:
        if j%2==0:
          print('*',end='')
        else:
          print('#',end='')
        j+=1
      print()
      i+=1

    (3)for 双项循环
    for i in range(10):
      for j in range(10):
        if j%2==0:
          print('*',end='')
        else:
          print('#',end='')
      print() 
    6.打印十行十列隔行换色小星星
    (1)while 单项循环
    i=0
    while i<100:
      if i//10%2==0:
        print('*',end='')
      else:
        print('#',end='')
      if i%10==9:
        print()
      i+=1
    (2)while双向循环
    i=0
    while i<10:
      j=0
      while j<10:
        if i%2==0:
          print('*',end='')
        else:
          print('#',end='')
        j+=1
      print()
      i+=1
    (3)for 双向循环
    for i in range(10):
      for j in range(10):
        if i%2==0:
          print('*',end='')
        else:
          print('#',end='')
      print()
    7.99乘法表
    方式一
    (1)while双项循环
    i=1
    while i<=9:
      j=1
      while j<=i:
        print('%d*%d=%2d ' % (i,j,i*j),end='')
        j+=1
      print()
      i+=1
    (2)for双向循环
    for i in range(1,10):
      for j in range(1,i+1):
        print('%d*%d=%2d ' % (i,j,i*j),end='')
      print()
    方式二
    (1)while双向循环
    i=9
    while i>=1:
      j=1
      while j<=i:
        print('%d*%d=%2d ' % (i,j,i*j),end='')
        j+=1
      print()
      i-=1
    (2)for双项循环
    for i in range(9,0,-1):
      for j in range(1,i+1):
        print('%d*%d=%2d ' % (i,j,i*j),end='')
      print()
    方式三
    (1)while双向循环
    i=1
    while i<=9:
      #打印空格
      j=1
      while j<=9-i:
        print('       ',end='')
        j+=1
      #打印表达式
      k=1
      while k<=i:
        print('%d*%d=%2d ' % (i,k,i*k),end='')
        k+=1
      #打印换行
      print()
      i+=1
    (2)for 双向循环
    for i in range(1,10):
      #打印空格
      for j in range(1,10-i):
        print('       ',end='')
      #打印表达式
      for k in range(1,i+1):
        print('%d*%d=%2d ' % (i,k,i*k),end='')
      #打印空格
      print()
    8.打印吉利数字100~999中111,222等123,345,等321,543等
    方式一
    (1)while循环
    i=100
    count=0
    while i<=999:
      gewei=i%10
      shiwei=i%100//10
      baiwei=i//100
      if gewei==shiwei and baiwei==shiwei:
        print(i)
        count+=1
      elif gewei==shiwei +1 and baiwei==shiwei-1:
        print(i)
        count+=1
      elif gewei==shiwei-1 and
    baiwei==shiwei+1:
        print(i)
      count+=1
      i+=1
    #打印个数
    print(count)
    (2)for 循环
    for i in range(100,1000):
      gewei=i%10
      baiwei=i//100
      shiwei=i%100//10
      if gewei==shiwei and baiwei==shiwei:
        print(i)
      elif gewei==shiwei+1 and baiwei==shiwei-1:
        print(i)
      elif gewei==shiwei-1 and baiwei==shiwei+1:
        print(i)
    方式二
    (1)while循环
    i=100
    while i<1000:
      strvar=str(i)
      gewei=int(strvar[-1])
      shiwei=int(strvar[1])
      baiwei=int(strvar[0])
      if gewei==shiwei and baiwei==shiwei:
        print(i)
      elif baiwei==shiwei+1 and gewei==shiwei-1:
        print(i)
      elif baiwei==shiwei-1 and gewei==shiwei+1:
        print(i)
      i+=1
    (2)for 循环
    for i in range(100,1000):
      strvar=str(i)
      gewei=int(strvar[-1])
      shiwei=int(strvar[1])
      baiwei=int(strvar[0])
      if gewei==shiwei and baiwei==shiwei:
        print(i)
      elif baiwei==shiwei+1 and gewei==shiwei-1:
        print(i)
      elif baiwei==shiwei-1 and gewei==shiwei+1:
        print(i)
    9.百钱买百鸡
    (1)while循环
    x=0
    count=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)
            count+=1
          z+=1
        y+=1
      x+=1
    print(count)
    (2)for 循环
    count=0
    for i in range(0,101):
      for j in range(0,34):
        for k in range(0,101):
          if i+j+k==100 and i+3*j+0.5*k==100:
            print(i,j,k)
            count+=1
    print(count)

  • 相关阅读:
    2.2 列表推导和生成器表达式
    1.2 如何使用特殊方法
    Selenium安装方法
    Python中Selenium的使用方法
    BeautifulSoup4的使用方法
    (转)Python中sort和sorted的区别和使用方法
    (转)Python中random模块的几个常用函数
    PR中我的常用快捷键
    二、交互式运行环境——REPL
    一、Node.js概述
  • 原文地址:https://www.cnblogs.com/l0928/p/13283482.html
Copyright © 2011-2022 走看看