黑马程序员-常用的必备代码段
------------------ Windows Phones 7手机开发、.Net培训、期待与您交流! ----------------------
九九乘法表
1 /// <summary> 2 /// 九九乘法表 3 /// </summary> 4 public static void JiuJiuChengFaBiao() 5 { 6 int i, j; 7 for (i = 1; i < 10; i++) 8 { 9 for (j = 1; j < 10; j++) 10 { 11 Console.Write("{0:00}*{1:00}={2,2} ", i, j, i * j); 12 } 13 Console.WriteLine(""); 14 } 15 Console.ReadLine(); 16 }
冒泡排序
1 /// <summary> 2 /// 冒泡排序 3 /// </summary> 4 public static void MaoPao() 5 { 6 int[] ints = { 321, 321, 32, 4, 324, 353654, 64, 7, 56, 75, 43, 4, 3, 42, 5, 43, 42, 5, 34, 2 }; 7 //输出排序前的结果 8 for (int i = 0; i < ints.Length; i++) 9 { 10 Console.WriteLine(ints[i]); 11 } 12 for (int i = 0; i < ints.Length; i++) 13 { 14 for (int j = i; j < ints.Length; j++) 15 { 16 //判断大小 17 if (ints[i] < ints[j]) 18 { 19 int temp = ints[i]; 20 ints[i] = ints[j]; 21 ints[j] = temp; 22 } 23 } 24 } 25 //输出排序后的结果 26 for (int i = 0; i < ints.Length; i++) 27 { 28 Console.WriteLine(ints[i]); 29 } 30 }
判断是否是质数
1 /// <summary> 2 /// 判断数字是否是质数/素数 3 /// </summary> 4 /// <param name="number">传入需要判断的数字</param> 5 /// <returns>返回是否是质数/素数的结果为 Bool类型</returns> 6 public static bool IsPrime(int number) 7 { 8 bool result = true; 9 for (int i = 2; i <= number / 2; i++) 10 { 11 if (number % i == 0) 12 result = false; 13 } 14 return result; 15 }
水仙花数
View Code
/// <summary> /// 100-1000内的水仙花数 /// </summary> public static void ShuiXianHuaShu() { int one = 0, two = 0, three = 0; for (int i = 100; i < 1000; i++) { three = i / 100; two = (i / 10) % 10; one = i % 10; if (i == one * one * one + two * two * two + three * three * three) Console.WriteLine(i); } }
------------------ Windows Phones 7手机开发、.Net培训、期待与您交流! ----------------------