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 ...
随机推荐
- JS继承的一些见解
JS继承的一些见解 js在es6之前的继承是五花八门的.而且要在项目中灵活运用面向对象写法也是有点别扭,更多的时候还是觉得面向过程的写法更为简单,效率也高.久而久之对js的继承每隔一段时间就会理解出现 ...
- JavaScript, 函数是实现异步的基础
昨天一朋友和我聊到JS中的异步和同步, 后来从异步和同步的问题中得出了函数的另一面, 觉得挺不错, 特此分享一下 ==== 追梦子: 聊天是同步还是异步 小A: 异步 小A: 和你聊还可以和别人聊 追 ...
- C# 删除文件夹
三种方法 1.这种方法简单,能删除文件夹内的所有文件(文件及子目录) DirectoryInfo di = new DirectoryInfo(string Path); di.Del ...
- [LeetCode] Image Smoother 图片平滑器
Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother t ...
- redis安装异常的解决的办法
在开始redis安装的时候,先废话一下 官网: 英文 :https://redis.io/ 中文 :http://www.redis.cn/ 首先我们需要一个linux服务器,当然windows也是可 ...
- orabbix插件监控oracle表空间问题
我们安装好orabbix插件后,查看Tablespaces监控项是发项值为none,第一反应是没监控成功,其实不然. 1.我们打开orabbix监控项参数的路径,下面为Tablespaces的sql代 ...
- mybatis学习一
1:ORM概念 ORM(OBJECT-RELATIONSHIP MAPPING) 即对象关系映射,是一种思想,实质是将数据库中的数据用对象的形式表现出来 JPA(JAVA PERSISIT ...
- [HNOI2012]与非
题目描述 NAND(与非)是一种二元逻辑运算,其运算结果为真当且仅当两个输入的布尔值不全为真.NAND运算的真值表如下(1表示真,0表示假): 两个非负整数的NAND是指将它们表示成二进制数,再在对应 ...
- 【USACO Feb 2014】Cow Decathlon
题目描述 约翰有 N 头奶牛,组成了一直队伍参加全能比赛.比赛一共有 N 项,每头奶牛必须参加一项比 赛,每项比赛也必须有一头奶牛参加.任何一头奶牛可以胜任任何一项比赛,但得分不一样.如果第 i 头奶 ...
- hdu3487 splay树
Play with Chain Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...