zoukankan      html  css  js  c++  java
  • 一个简单的python程序

    题目:求1+2-3+4-5+6...99的和

    解答:

    首先可以从题目中看出除了1之外所有的奇数都是负数,所有的偶数都是正数,具体代码如下:

    方法一:

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    # Author:Lisa Li
    
    # 求1+2-3+4-5+6...99的和
    count = 1                   # 设定初始值
    s1 = 0                      # 接收计算所有偶数的变量
    s2 = 0                      # 接收计算所有基数的变量
    while count <= 99:          # 取出1-99所有的数字
        if count % 2 == 0:      # 取出1-99所有数的偶数
            s1 = s1 + count     # 取出的偶数进行运算
        else:                   # 取出1-99内所有的奇数
            s2 = s2 - count      # 取出的奇数进行运算
        s = s1 + s2              # 将计算出来的所有偶数的值和奇数的值进行相加
        print(s)                 # 打印结果
        count += 1               # 自增1
    

      输出结果:

    -1
    1
    -2
    2
    -3
    3
    -4
    4
    -5
    5
    -6
    6
    -7
    7
    -8
    8
    -9
    9
    -10
    10
    -11
    11
    -12
    12
    -13
    13
    -14
    14
    -15
    15
    -16
    16
    -17
    17
    -18
    18
    -19
    19
    -20
    20
    -21
    21
    -22
    22
    -23
    23
    -24
    24
    -25
    25
    -26
    26
    -27
    27
    -28
    28
    -29
    29
    -30
    30
    -31
    31
    -32
    32
    -33
    33
    -34
    34
    -35
    35
    -36
    36
    -37
    37
    -38
    38
    -39
    39
    -40
    40
    -41
    41
    -42
    42
    -43
    43
    -44
    44
    -45
    45
    -46
    46
    -47
    47
    -48
    48
    -49
    49
    -50
    View Code

    方法二:

    # 求1+2-3+4-5+6...99的和
    
    s1 = 0                          #定义接收所有偶数的变量
    s2 = 0                           #定义接收所有奇数的变量
    for count in range(1,100):       #取1-99的所有数字,顾头不顾尾
        # print(count)
        if count % 2 == 0:              #取1-99的所有偶数
            s1 = s1 + count             # 将1-99所有偶数相加
        else:                           #取1-99所有奇数
            s2 = s2 - count             # 计算1-99所有奇数的值
        s = s1 + s2                     #把所有偶数的值和奇数的值进行相加
        print(s)                        #输出结果
    

      输出结果:

    -1
    1
    -2
    2
    -3
    3
    -4
    4
    -5
    5
    -6
    6
    -7
    7
    -8
    8
    -9
    9
    -10
    10
    -11
    11
    -12
    12
    -13
    13
    -14
    14
    -15
    15
    -16
    16
    -17
    17
    -18
    18
    -19
    19
    -20
    20
    -21
    21
    -22
    22
    -23
    23
    -24
    24
    -25
    25
    -26
    26
    -27
    27
    -28
    28
    -29
    29
    -30
    30
    -31
    31
    -32
    32
    -33
    33
    -34
    34
    -35
    35
    -36
    36
    -37
    37
    -38
    38
    -39
    39
    -40
    40
    -41
    41
    -42
    42
    -43
    43
    -44
    44
    -45
    45
    -46
    46
    -47
    47
    -48
    48
    -49
    49
    -50
    View Code
  • 相关阅读:
    poj 1328 Radar Installation (贪心)
    hdu 2037 今年暑假不AC (贪心)
    poj 2965 The Pilots Brothers' refrigerator (dfs)
    poj 1753 Flip Game (dfs)
    hdu 2838 Cow Sorting (树状数组)
    hdu 1058 Humble Numbers (DP)
    hdu 1069 Monkey and Banana (DP)
    hdu 1087 Super Jumping! Jumping! Jumping! (DP)
    必须知道的.NET FrameWork
    使用记事本+CSC编译程序
  • 原文地址:https://www.cnblogs.com/yujiemeigui/p/7061992.html
Copyright © 2011-2022 走看看