Swift完成fizz buzz test
看到一篇文章上说,很多貌似看过很多本编程书的童鞋连简单的fizz buzz测试都完不成.
不知道fizz buzz test为何物的,建议自行搜之.
测试要求是,编写满足以下条件的代码:
Write a program that prints the numbers from 1 to 100. But
for multiples of three print “Fizz” instead of the number
which are multiples of both three and five print
“FizzBuzz”.
更通俗的说就是:
For each integer between 1 and 100, inclusive:
If the number is divisible by '3', then print "Fizz"
If the number is divisible by '5', then print "Buzz"
If the number is divisible by both '3' and '5', then print "FizzBuzz"
Otherwise, print the number.
本猫用Swift的解决方案如下:
for x in 1...100{
if x % 3 == 0 && x % 5 == 0{
print("FizzBuzz")
}else if x % 3 == 0{
print("Fizz")
}else if x % 5 == 0{
print("Buzz")
}else{
print(x)
}
}
好吧,我承认是超级简单…我是有够无聊… ;[
Swift完成fizz buzz test的更多相关文章
- [Swift]LeetCode412. Fizz Buzz
Write a program that outputs the string representation of numbers from 1 to n. But for multiples of ...
- [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 ...
- Fizz Buzz 面试题
在CSDN上看到一篇文章<软件工程师如何笑着活下去>,本来是想看对这个行业的一些评价和信息,不曾检索到关于Fizz Buzz的面试题,上网搜了一下,顿感兴趣.留下此文,以表回忆. java ...
随机推荐
- MySQL协议学习(1):准备工作
MySQL Client/Server协议 准确的说应该是MySQL Client/Server协议,另一个叫X Protocol的暂不涉及.地址如下:MySQL Client/Server Prot ...
- vue-cli 体验
vue-cli 是一个官方发布 vue.js 项目脚手架,使用 vue-cli 可以快速创建 vue 项目,GitHub地址是:https://github.com/vuejs/vue-cli 一. ...
- PHP性能优化利器:生成器 yield理解
如果是做Python或者其他语言的小伙伴,对于生成器应该不陌生.但很多PHP开发者或许都不知道生成器这个功能,可能是因为生成器是PHP 5.5.0才引入的功能,也可以是生成器作用不是很明显.但是,生成 ...
- HTML笔记04---计时事件
JavaScript运动01 计时事件 1.语法:var t=setTimeout("javascript语句",毫秒); setTimeout() 方法会返回某个值.在上面的语句 ...
- 音频压缩编码 opus 附完整C++代码示例
绝大数人都知道mp3格式编码,以及aac,amr等压缩格式编码. 而在语音通信界有一个强悍的音频格式编码opus. 经过实测,压缩比最高可以达到1:10. 100KB 压缩后 10KB 虽然是有损压缩 ...
- [LeetCode] Next Greater Element III 下一个较大的元素之三
Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly th ...
- 一个web程序员的年终总结
2017年年终总结(就是一个程序员的瞎叨叨): 从来到中科院到现在,很开心可以在这留下来.毕竟对于我来说,这里符合我对自己毕业后前两年的规划.我是一个很慢的人,特别是对于我想做好的事情,我会非常认真仔 ...
- ios、移动端 input type=date无法点击的问题解决方法
正常用input type = "text",获取焦点的时候,将type 改成 date即可. <div class="form-item"> &l ...
- 洛谷P3980:[NOI2008]志愿者招募
线性规划: #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring&g ...
- 【Git】Git工具常用命令
GitHub使用指南 一.把本地代码上传到GitHub 0. 提前配置好上传地址 [git config --global user.name "username"] [git c ...