标签: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”.
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;
}
}
标签:fizzbuzz ret out for index add 数组 turn code
原文地址:http://www.cnblogs.com/xiejunzhao/p/41c51e6d3c43ef10a17fdb9d8e03c658.html