• 练习—8.17 while循环


    练习题:

    1. 打印 1-99之间的所有奇数(循环,每个数递增)

    方法一:

    start = 1
    while start <= 99:
         n = start % 2
         if n == 1:
            print(start)
            start = start + 1
         else:
            start = start + 1

    方法二:

    start = 1
    while start <= 99:
         n = start % 2
         if n == 1:
            print(start)
         start = start + 1

    2. 打印 1-99之间所有能被3整除数

    start = 1

    while start <= 99:
         n = start % 3
         if n == 0: 
            print(start)
         start = start + 1

    3. 打印 1-99之间所有数总和

    start = 1
    num = 0
      while start <= 99:
         num = num + start
         start = start + 1
      print(num)

    4. 打印 1-99之间所有奇数总和
    start = 1
    num = 0
    while start <= 99:
         n = start % 2
         if n == 1:
            num = num + start
       start = start + 1
      print(num)

    5. 打印 1-2+3-4+5-6+7 ...+99  总和

    start = 1
    num = 0
     while start <= 99:
         n = start % 2
         if n == 1:
            num = num + start
         else:
            num = num - start
         start = start + 1

    print(num) 
       

  • 相关阅读:
    ZOJ 3556
    ZOJ 2836
    HDU 2841
    HDU 4135
    POJ 3695
    POJ 2773
    HDU 4407
    HDU 1796
    ZOJ 3688
    ZOJ 3687
  • 原文地址:https://www.cnblogs.com/xu1686318405/p/7502952.html
Copyright © 2020-2023  润新知