看到那么多人看到前面这么糟粕的代码各种不忍直视后,楼主还是把最终实现放在页首吧.
Console.WriteLine("说出三个不同的特殊数,用','隔开 ");
string[] s1 = Console.ReadLine().Split(',');
for (int i = 1; i < 101; i++)
{
if (i.ToString().Contains(s1[0])) { Console.WriteLine("Fizz"); }
else { Console.WriteLine((i % int.Parse(s1[0]) == 0 ? "Fizz" : "") + (i % int.Parse(s1[1]) == 0 ? "Buzz" : "") + (i % int.Parse(s1[2]) == 0 ? "Whizz" : "") + ((i % int.Parse(s1[0]) != 0 && i % int.Parse(s1[1]) != 0 && i % int.Parse(s1[2]) != 0) ? i.ToString() : "")); }
}
Console.ReadKey();
其实我想表达的意思是学习更多的是循序渐进而不是天才般的灵光乍现,不过不知谁懂呢.
一开始楼主傻BB直接求解,思路惨不忍睹,各种循环啊有木有?还写了个计算是否整除扩展方法以为重复复用很了不起啊(其实是傻...)
二逼青年的代码
1
#region so1
2 int[] count = new int[ 100];
3 string[] s4 = new string[] { " Fizz ", " Buzz ", " Whizz " };
4 for ( int i = 0; i < count.Length; i++)
5 {
6 count[i] = i + 1;
7 if (count[i].compa(s2[ 0]))
8 {
9 result[i] = s4[ 0];
10 if (count[i].compa(s2[ 1]))
11 { result[i] = s4[ 0] + s4[ 1]; }
12 }
13 else
14 {
15 if (count[i].compa(s2[ 2]))
16 {
17 result[i] = s4[ 2];
18 if (count[i].compa(s2[ 1]))
19 {
20 result[i] = s4[ 1] + s4[ 2];
21 if (count[i].compa(s2[ 0]))
22 { result[i] = s4[ 0] + s4[ 1] + s4[ 2]; }
23 }
24 else
25 {
26 if (count[i].compa(s2[ 0]))
27 { result[i] = s4[ 0] + s4[ 1]; }
28 }
29 }
30 else
31 { result[i] = (i + 1).ToString(); }
32 }
33 }
34 for ( int i = 0; i < 10; i++)
35 { result[s2[ 0] + i * 10 - 1] = s4[ 0]; result[i + s2[ 0] * 10 - 1] = s4[ 0]; }
36 foreach ( var item in result)
37 { Console.WriteLine(item); }
38 #endregion
2 int[] count = new int[ 100];
3 string[] s4 = new string[] { " Fizz ", " Buzz ", " Whizz " };
4 for ( int i = 0; i < count.Length; i++)
5 {
6 count[i] = i + 1;
7 if (count[i].compa(s2[ 0]))
8 {
9 result[i] = s4[ 0];
10 if (count[i].compa(s2[ 1]))
11 { result[i] = s4[ 0] + s4[ 1]; }
12 }
13 else
14 {
15 if (count[i].compa(s2[ 2]))
16 {
17 result[i] = s4[ 2];
18 if (count[i].compa(s2[ 1]))
19 {
20 result[i] = s4[ 1] + s4[ 2];
21 if (count[i].compa(s2[ 0]))
22 { result[i] = s4[ 0] + s4[ 1] + s4[ 2]; }
23 }
24 else
25 {
26 if (count[i].compa(s2[ 0]))
27 { result[i] = s4[ 0] + s4[ 1]; }
28 }
29 }
30 else
31 { result[i] = (i + 1).ToString(); }
32 }
33 }
34 for ( int i = 0; i < 10; i++)
35 { result[s2[ 0] + i * 10 - 1] = s4[ 0]; result[i + s2[ 0] * 10 - 1] = s4[ 0]; }
36 foreach ( var item in result)
37 { Console.WriteLine(item); }
38 #endregion
后来楼主想了一下,感觉判断是否整除可以复用,然后一般的值先赋过去,虽然这样会重复,但是由于集合是越来越小的所以不会出差错,而在a,b,c集合外的值作为另外一个判断,赋普通的值给他,这样代码就如下了:
普通青年的代码
#region so02
int sh = 1;
for ( int i = 0; i < 10; i++)
{
int i2 = 0;
for (i2 = 0; i2 < 10; i2++)
{
bool b1 = sh.compa(s2[ 0]);
bool b2 = sh.compa(s2[ 1]);
bool b3 = sh.compa(s2[ 2]);
if (b1) { result[i * 10 + i2] = " Fizz "; }
if (b2) { result[i * 10 + i2] = " Buzz "; }
if (b3) { result[i * 10 + i2] = " Whizz "; }
if (b1 && b2) { result[i * 10 + i2] = " FizzBuzz "; }
if (b1 && b3) { result[i * 10 + i2] = " FizzWhizz "; }
if (b2 && b3) { result[i * 10 + i2] = " BuzzWhizz "; }
if (b1 && b2 && b3) { result[i * 10 + i2] = " FizzBuzzWhizz "; }
if (i2 == s2[ 0]) { result[i * 10 + i2] = " Fizz "; }
if (!(b1 || b2 || b3))
{ result[i * 10 + i2] = sh.ToString(); }
sh++;
}
if (i == s2[ 0])
{
for ( int i3 = 0; i3 < 10; i3++)
{ result[i * 10 + i3 - 1] = " Fizz "; }
}
}
#endregion
int sh = 1;
for ( int i = 0; i < 10; i++)
{
int i2 = 0;
for (i2 = 0; i2 < 10; i2++)
{
bool b1 = sh.compa(s2[ 0]);
bool b2 = sh.compa(s2[ 1]);
bool b3 = sh.compa(s2[ 2]);
if (b1) { result[i * 10 + i2] = " Fizz "; }
if (b2) { result[i * 10 + i2] = " Buzz "; }
if (b3) { result[i * 10 + i2] = " Whizz "; }
if (b1 && b2) { result[i * 10 + i2] = " FizzBuzz "; }
if (b1 && b3) { result[i * 10 + i2] = " FizzWhizz "; }
if (b2 && b3) { result[i * 10 + i2] = " BuzzWhizz "; }
if (b1 && b2 && b3) { result[i * 10 + i2] = " FizzBuzzWhizz "; }
if (i2 == s2[ 0]) { result[i * 10 + i2] = " Fizz "; }
if (!(b1 || b2 || b3))
{ result[i * 10 + i2] = sh.ToString(); }
sh++;
}
if (i == s2[ 0])
{
for ( int i3 = 0; i3 < 10; i3++)
{ result[i * 10 + i3 - 1] = " Fizz "; }
}
}
#endregion
(是否有误未测试,但大体是这个思路) .
后来楼主再回去看那个页面,人家说10行代码就能搞定,楼主看着这普通青年的代码表示蛋疼,于是请教一下在中大的基友.人家读数学专业的就是不一样,给的思路也是天马行空,于是借鉴一下传说中的至简代码就出来了.
文艺青年的代码
1 Console.WriteLine(
"
说出三个不同的特殊数,用','隔开
");
2 string[] s1 = Console.ReadLine().Split( ' , ');
3 int[] s2 = new int[] { int.Parse(s1[ 0]), int.Parse(s1[ 1]), int.Parse(s1[ 2]) };
4 string[] result = new string[ 100];
5 for ( int i = 1; i < 101; i++) { result[i - 1] = (i % s2[ 0] == 0 ? " Fizz " : "") + (i % s2[ 1] == 0 ? " Buzz " : "") + (i % s2[ 2] == 0 ? " Whizz " : "") + ((i % s2[ 0] != 0 && i % s2[ 1] != 0 && i % s2[ 2] != 0) ? i.ToString() : ""); }
6 for ( int i3 = 0; i3 < 10; i3++) { result[s2[ 0] * 10 + i3 - 1] = result[i3 * 10 + s2[ 0] - 1] = " Fizz "; }
7 foreach ( var item in result) { Console.WriteLine(item); }
8 Console.ReadKey();
2 string[] s1 = Console.ReadLine().Split( ' , ');
3 int[] s2 = new int[] { int.Parse(s1[ 0]), int.Parse(s1[ 1]), int.Parse(s1[ 2]) };
4 string[] result = new string[ 100];
5 for ( int i = 1; i < 101; i++) { result[i - 1] = (i % s2[ 0] == 0 ? " Fizz " : "") + (i % s2[ 1] == 0 ? " Buzz " : "") + (i % s2[ 2] == 0 ? " Whizz " : "") + ((i % s2[ 0] != 0 && i % s2[ 1] != 0 && i % s2[ 2] != 0) ? i.ToString() : ""); }
6 for ( int i3 = 0; i3 < 10; i3++) { result[s2[ 0] * 10 + i3 - 1] = result[i3 * 10 + s2[ 0] - 1] = " Fizz "; }
7 foreach ( var item in result) { Console.WriteLine(item); }
8 Console.ReadKey();
思路就是规则3和4通过活用三元运算符+字符串同时拼接处理.第一个for循环为规则3和4赋值,如果满足整除条件,输出对应单词,这样能被N(1到3)整除的数字对应的单词都能赋到,但是有一种情况就是整除条件都不满足,也就是说是一个普通的数字,而前面根据三元运算符得到的答案都是“”,所已最后的条件满足的话输出普通的数字。
然后是规则5。规则5是最重要的,所已在最后才为其赋值。这个没什么好说的,想一想都知道规则5每种条件都包含10个数,所已在0到10之间循环,同时为个数满足以及十位数满足的数赋值就行了.代码控制在10行代码啊有木有(我不知道10行的定义是什么这样稍微有点勉强不过最少也有8行吧,哈哈 )?
当然是假定用户不捣乱,一开始不输入错的情况。加个验证?那你们就慢慢验吧。
下部就不截了。