首页 > 编程语言 > 详细

412.数组下标的倍数 Fizz Buzz

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

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!