1 using System.Collections.Generic; 2 using System.ComponentModel.Design; 3 using System.Linq; 4 5 namespace TestDelay 6 { 7 internal class Program 8 { 9 static void Main() 10 { 11 int[] arry = new int[] { 12, 32, 434, 56, 2, 1, 9, 0 }; 12 BubbleSort(arry); 13 Console.WriteLine("冒泡排序:" + string.Join(",", arry)); 14 15 Demo1(7); 16 Reverse("I am a good man"); 17 18 Demo2(4); 19 20 Demo3(30); 21 } 22 /// <summary> 23 /// 冒泡排序 24 /// </summary> 25 /// <param name="arry"></param> 26 public static void BubbleSort(int[] arry) 27 { 28 for (int i = 0; i < arry.Length - 1; i++) 29 { 30 for (int j = i + 1; j < arry.Length; j++) 31 { 32 if (arry[i] > arry[j]) 33 { 34 var temp = arry[i]; 35 arry[i] = arry[j]; 36 arry[j] = temp; 37 } 38 } 39 } 40 } 41 42 /// 求以下表达式的值,写出您想到的一种或几种实现方法: 1-2+3-4+……+m 43 /// 拆分为(1-2)+(3-4)+(5-6)+...+m 44 /// </summary> 45 /// <param name="m"></param> 46 public static void Demo1(int m) 47 { 48 #region 第一种方式 49 int result1 = 0; 50 if (m % 2 == 0) 51 result1 = -(m / 2); 52 else 53 result1 = -(m - 1) / 2 + m; 54 #endregion 55 #region 第二种方式 56 int result2 = 0; 57 for (int i = 1; i <= m; i++) 58 { 59 if (i % 2 == 0) 60 result2 = result2 - i; 61 else 62 result2 = result2 + i; 63 } 64 #endregion 65 Console.WriteLine(result1 == result2); 66 } 67 68 /// <summary> 69 /// 有一个字符串 "I am a good man",设计一个函数,返回 "man good a am I"。 70 /// </summary> 71 /// <param name="str"></param> 72 public static void Reverse(string str) 73 { 74 var strArry = str.Split(" "); 75 var result1 = string.Join(" ", strArry.Reverse()); 76 List<string> list = new List<string>(); 77 for (int i = strArry.Length - 1; i >= 0; i--) 78 { 79 list.Add(strArry[i]); 80 } 81 var result2 = string.Join(" ", list); 82 Console.WriteLine(result1 == result2); 83 } 84 85 /// <summary> 86 /// 有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 87 /// </summary> 88 /// <param name="number"></param> 89 public static void Demo2(int number) 90 { 91 List<string> list = new List<string>(); 92 for (int b = 1; b <= number; b++) 93 { 94 for (int s = 1; s <= number; s++) 95 { 96 if (b != s) 97 { 98 for (int g = 1; g <= number; g++) 99 { 100 if (b != s && b != g && s != g) 101 list.Add($"{b}{s}{g}"); 102 } 103 } 104 } 105 } 106 Console.WriteLine($"1-{number},共有:{list.Count}个不重复三位数,分别是:{string.Join(",", list)}"); 107 } 108 109 110 /// <summary> 111 /// 1、1、2、3、5、8、13、21、34,....⽤C#递归写出算法,算出第30个数。 112 /// </summary> 113 /// <param name="number"></param> 114 public static void Demo3(int number) 115 { 116 List<int> list= new List<int>(); 117 Recursion(0, list); 118 var result = string.Join(" ", list); 119 } 120 /// <summary> 121 /// 1、1、2、3、5、8、13、21、34,....⽤C#递归写出算法,算出第30个数 122 /// </summary> 123 /// <param name="number"></param> 124 /// <param name="list"></param> 125 public static void Recursion(int number, List<int> list) 126 { 127 if (number <= 1) 128 list.Add(1); 129 else 130 list.Add(list[list.Count - 2] + list[list.Count - 1]); 131 if (number < 30) 132 Recursion(number + 1, list); 133 } 134 } 135 136 137 138 139 140 141 142 143 144 }
标签:c#,List,void,list,int,算法,static,Net,arry From: https://www.cnblogs.com/aaaaq/p/17987764