412.数组下标的倍数 Fizz Buzz
Posted Long Long Journey
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了412.数组下标的倍数 Fizz Buzz相关的知识,希望对你有一定的参考价值。
Write a program that outputs the string representation of numbers from 1 to n.
But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.
public class Solution {
public IList<string> FizzBuzz(int n) {
List<string> list = new List<string>();
for (int index = 1; index <= n; index++) {//要从1开始,另外要注意0%任意数字都是0
if (index % 3 == 0 && index % 5 == 0) {
list.Add("FizzBuzz");
} else if (index % 3 == 0) {
list.Add("Fizz");
} else if (index % 5 == 0) {
list.Add("Buzz");
} else {
list.Add(index.ToString());
}
}
return list;
}
}
以上是关于412.数组下标的倍数 Fizz Buzz的主要内容,如果未能解决你的问题,请参考以下文章