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

Fizz Buzz 问题

时间:2016-06-12 03:19:15      阅读:473      评论:0      收藏:0      [点我收藏+]

标签:

题目描述:给你一个整数n. 从 1 到 n 按照下面的规则打印每个数:

如果这个数被3整除,打印fizz.

如果这个数被5整除,打印buzz.

如果这个数能同时被3和5整除,打印fizz buzz.

样例:比如 n = 15, 返回一个字符串数组:

[
  "1", "2", "fizz",
  "4", "buzz", "fizz",
  "7", "8", "fizz",
  "buzz", "11", "fizz",
  "13", "14", "fizz buzz"

]


lintcode中最简单的题目,不讲了,把代码放这:

class Solution:
    """
    @param n: An integer as description
    @return: A list of strings.
    For example, if n = 7, your code should return
        ["1", "2", "fizz", "4", "buzz", "fizz", "7"]
    """
    def fizzBuzz(self, n):
        results = []
        for i in range(1, n+1):
            if i % 15 == 0:
                results.append("fizz buzz")
            elif i % 5 == 0:
                results.append("buzz")
            elif i % 3 == 0:
                results.append("fizz")
            else:
                results.append(str(i))
        return results


Fizz Buzz 问题

标签:

原文地址:http://blog.csdn.net/guoziqing506/article/details/51601284

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