首页 > 代码库 > 412.数组下标的倍数 Fizz Buzz

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



来自为知笔记(Wiz)


412.数组下标的倍数 Fizz Buzz