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”.


  1. public class Solution {
  2. public IList<string> FizzBuzz(int n) {
  3. List<string> list = new List<string>();
  4. for (int index = 1; index <= n; index++) {//要从1开始,另外要注意0%任意数字都是0
  5. if (index % 3 == 0 && index % 5 == 0) {
  6. list.Add("FizzBuzz");
  7. } else if (index % 3 == 0) {
  8. list.Add("Fizz");
  9. } else if (index % 5 == 0) {
  10. list.Add("Buzz");
  11. } else {
  12. list.Add(index.ToString());
  13. }
  14. }
  15. return list;
  16. }
  17. }





以上是关于412.数组下标的倍数 Fizz Buzz的主要内容,如果未能解决你的问题,请参考以下文章

快乐水题412. Fizz Buzz

412-Fizz Buzz

412. Fizz Buzz

LeetCode|412. Fizz Buzz

LeetCode 412 Fizz Buzz

题目地址(412. Fizz Buzz)