Fizz Buzz 问题
要求:
给你一个整数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"
]
package main import (
"fmt"
"strconv"
) func main() {
var n int =
res := fizz(n)
fmt.Println("Fizee Buzz: ", res)
} func fizz(n int) []string {
var res = []string{}
if n <= {
return res
} for i:=;i<=;i++ {
if i%== && i%== {//i%15
res = append(res, "fizz buzz")
} else if i%== {
res = append(res, "fizz")
} else if i%== {
res = append(res, "buzz")
} else {
res = append(res, strconv.FormatInt(int64(i), ))
}
}
return res
}
Fizz Buzz 问题的更多相关文章
- [LeetCode] Fizz Buzz 嘶嘶嗡嗡
Write a program that outputs the string representation of numbers from 1 to n. But for multiples of ...
- Lintcode 9.Fizz Buzz 问题
------------------------ AC代码: class Solution { /** * param n: As description. * return: A list of s ...
- LeetCode 412. Fizz Buzz
Problem: Write a program that outputs the string representation of numbers from 1 to n. But for mult ...
- LeetCode Fizz Buzz
原题链接在这里:https://leetcode.com/problems/fizz-buzz/ 题目: Write a program that outputs the string represe ...
- Fizz Buzz
class Solution { public: /** * param n: As description. * return: A list of strings. */ vector<st ...
- LintCode (9)Fizz Buzz
下面是AC代码,C++风格: class Solution { public: vector<string> fizzBuzz(int N) { vector<string> ...
- [重构到模式-Chain of Responsibility Pattern]把Fizz Buzz招式重构到责任链模式
写一段程序从1打印到100,但是遇到3的倍数时打印Fizz,遇到5的倍数时打印Buzz,遇到即是3的倍数同时也是5的倍数时打印FizzBuzz.例如: 1 2 Fizz 4 Buzz Fizz 7 8 ...
- Swift完成fizz buzz test
看到一篇文章上说,很多貌似看过很多本编程书的童鞋连简单的fizz buzz测试都完不成. 不知道fizz buzz test为何物的,建议自行搜之. 测试要求是,编写满足以下条件的代码: Write ...
- [Swift]LeetCode412. Fizz Buzz
Write a program that outputs the string representation of numbers from 1 to n. But for multiples of ...
- Fizz Buzz 面试题
在CSDN上看到一篇文章<软件工程师如何笑着活下去>,本来是想看对这个行业的一些评价和信息,不曾检索到关于Fizz Buzz的面试题,上网搜了一下,顿感兴趣.留下此文,以表回忆. java ...
随机推荐
- UI界面按钮增强
(我是菜鸟,知道的少) 1.有HTML页面代码的,可以编辑 <%@page language="abap" %> <%@extension name=" ...
- Homebrew 安装mysql
在mac上安装软件,无疑安装一个brew是个很好的选择,关于brew是什么,怎么安装建议去brew官网查看, 附上地址:brew官网 还有一篇博文 http://www.cnblogs.com/xd ...
- ajax实现给JavaScript中全局变量赋值(转)
原文地址:ajax实现给JavaScript中全局变量赋值 问题简化: <script type="text/javascript"> var a=1 ; functi ...
- centos命令行系列之升级glibc到
1.从http://ftp.gnu.org/gnu/glibc/glibc-2.17.tar.gz 下载文件 2.安装部署 [root@kafzook1 /]# tar -xf glibc-2.17. ...
- Git中的文件上传、修改、撤消修改和删除
1.添加文件.提交文件 1.1在learngit目录下创建一个readme.txt文件,并且输入内容. 1.2添加文件到版本库learngit 1.2.1使用git add 文件告诉Git把文件添加 ...
- Linux定时计划(crontab)使用说明
一.设置定时计划步骤 第一步,编缉计划文件:crontab -e 第二步,在文件中写入计划,格式如:minute hour day month week command.如0 8 * * * sh / ...
- 放弃Dubbo,选择最流行的Spring Cloud微服务架构实践与经验总结
http://developer.51cto.com/art/201710/554633.htm Spring Cloud 在国内中小型公司能用起来吗?从 2016 年初一直到现在,我们在这条路上已经 ...
- pl/sql 如何将Excel文件数据导入oracle的数据表?
1.准备导入数据的excel文件 注意:excel列名和数据表列名必须相同,excel文件sheet2和sheet3可以删除 1)excel文件格式 2)数据表格式 2.打开pl/sql ,找到工具- ...
- IntelliJ Idea 快捷键列表
最重要:1Ctrl+Alt+Shift+T:查找类2重构3提取父类 Ctrl+Shift + Enter,语句完成“!”,否定完成,输入表达式时按 “!”键Ctrl+E,最近的文件Ctrl+Shift ...
- linux网络操作 netstat命令
关闭与启动网卡 ifdown 网卡设备名 #禁用该网卡设备 ifup网卡设备名 #启用该网卡设备 查看网络状态 netstat 命令 -t 列出tcp协议端口 -u 列出udp协议端口 -n 不 ...