美文网首页
抓瞎: 穷尽和求余匹配

抓瞎: 穷尽和求余匹配

作者: 极光火狐狸 | 来源:发表于2018-09-26 18:53 被阅读33次

    python

    
    # -.- coding:utf-8 -.-
    # 遍历1到100
    # 如果数字能被3整除就打印Fizz,
    # 如果数字能被5整除就打印Buzz,
    # 如果数字能同时被3和5整除就打印FizzBuzz,
    # 剩下的就打印数字本身.
    from __future__ import print_function
    
    
    def main():
        for i in range(1, 101):
            fz = (i % 3, i % 5)
            if fz == (0, 0):
                print("FizzBuzz")
            elif fz[0] == 0:
                print("Fizz")
            elif fz[1] == 0:
                print("Buzz")
            else:
                print(i)
    
    
    if __name__ == '__main__':
    main()
    

    rust

    // 遍历1到100
    // 如果数字能被3整除就打印Fizz,
    // 如果数字能被5整除就打印Buzz,
    // 如果数字能同时被3和5整除就打印FizzBuzz,
    // 剩下的就打印数字本身.
    
    fn main() {
        for i in 1..101 {
            match (i % 3, i %5) {
                (0, 0) => println!("FizzBuzz"),
                (0, _) => println!("Fizz"),
                (_, 0) => println!("Buzz"),
                _ => println!("{}", i)
            }
        }
    }
    

    输出

    1
    2
    Fizz
    4
    Buzz
    Fizz
    7
    8
    Fizz
    Buzz
    11
    Fizz
    13
    14
    FizzBuzz
    16
    17
    Fizz
    19
    Buzz
    Fizz
    22
    23
    Fizz
    Buzz
    26
    Fizz
    28
    29
    FizzBuzz
    31
    32
    Fizz
    34
    Buzz
    Fizz
    37
    38
    Fizz
    Buzz
    41
    Fizz
    43
    44
    FizzBuzz
    46
    47
    Fizz
    49
    Buzz
    Fizz
    52
    53
    Fizz
    Buzz
    56
    Fizz
    58
    59
    FizzBuzz
    61
    62
    Fizz
    64
    Buzz
    Fizz
    67
    68
    Fizz
    Buzz
    71
    Fizz
    73
    74
    FizzBuzz
    76
    77
    Fizz
    79
    Buzz
    Fizz
    82
    83
    Fizz
    Buzz
    86
    Fizz
    88
    89
    FizzBuzz
    91
    92
    Fizz
    94
    Buzz
    Fizz
    97
    98
    Fizz
    Buzz
    

    相关文章

      网友评论

          本文标题:抓瞎: 穷尽和求余匹配

          本文链接:https://www.haomeiwen.com/subject/izyjoftx.html