C#算法
九九乘法表
1 static void Main(string[] args) 2 { 3 for (int i = 1; i < 10; i++) 4 { 5 for (int j = 1; j <= i; j++) 6 { 7 Console.Write("{0}*{1}={2} ",j,i,i*j); 8 } 9 Console.WriteLine(); 10 } 11 Console.ReadLine(); 12 }
水仙花数
1 List<int> list = new List<int>(); 2 int baiWei = 0; 3 int shiWei = 0; 4 int geWei = 0; 5 for (int i = 100; i < 1000; i++) 6 { 7 baiWei = i / 100; 8 shiWei = (i - baiWei * 100) / 10; 9 geWei = i % 10; 10 if (Math.Pow(baiWei, 3) + Math.Pow(shiWei, 3) + Math.Pow(geWei, 3) == i) 11 { 12 list.Add(i); 13 } 14 } 15 return list;
冒泡排序
1 int temp = 0; 2 List<int> list = new List<int>() { 1, 2, 34, 5, 6, 10, 7, 9, 100 }; 3 for (int i = 0; i < list.Count - 1; i++)//一共需要整理的次数 4 { 5 for (int j = 0; j < list.Count - 1; j++)//循环遍历每一次比较两个数值 6 { 7 if (list[j] > list[j + 1]) 8 { 9 temp = list[j + 1]; 10 list[j + 1] = list[j]; 11 list[j] = temp; 12 } 13 } 14 } 15 foreach (var i in list) 16 { 17 Console.WriteLine(i); 18 } 19 Console.ReadLine();
浙公网安备 33010602011771号