码迷,mamicode.com
首页 > 编程语言 > 详细

412.数组下标的倍数 Fizz Buzz

时间:2017-01-10 23:35:01      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:fizzbuzz   ret   out   for   index   add   数组   turn   code   

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

标签:fizzbuzz   ret   out   for   index   add   数组   turn   code   

原文地址:http://www.cnblogs.com/xiejunzhao/p/41c51e6d3c43ef10a17fdb9d8e03c658.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!