c#学习笔记1-简单算法
1 using System; 2 namespace Demo 3 { 4 class Studycs 5 { 6 public static void Main(String[] args) 7 { 8 // String result = Revert("abcdef"); 9 int[] numbers = { 8, 2, 3, 4, 6 }; 10 // int result = searchTree(numbers, 7); 11 12 int[] result=sortSelect(numbers); 13 foreach(int item in result) 14 { 15 Console.WriteLine(item); 16 } 17 // char c ='c'; 18 // String result = toCompareChar("aABCDEFghiJKm"); 19 20 Console.ReadKey(); 21 } 22 23 /// <summary> 24 /// 翻转字符串 25 /// </summary> 26 /// <param name="str"></param> 27 /// <returns></returns> 28 static String Revert(String str) 29 { 30 String result = String.Empty; 31 char[] array = str.ToCharArray(); 32 for(int i = array.Length - 1; i >= 0; i--) 33 { 34 result += array[i]; 35 } 36 return result; 37 } 38 39 /// <summary> 40 /// 二分查找 41 /// </summary> 42 /// <param name="array"></param> 43 /// <param name="number"></param> 44 /// <returns></returns> 45 static int searchTree(int[] array,int number) 46 { 47 int min =0; 48 int max = array.Length-1; 49 int index =-1; 50 while (max >= min) 51 { 52 int result = (min + max) / 2; 53 if (array[result] == number) 54 { 55 index = result; 56 break; 57 } 58 else if (array[result] > number) 59 { 60 max = result - 1; 61 } 62 else 63 { 64 min = result + 1; 65 } 66 } 67 return index; 68 69 } 70 71 /// <summary> 72 /// 一维数组最大值 73 /// </summary> 74 /// <param name="array"></param> 75 /// <returns></returns> 76 static int Max(int[] array) 77 { 78 int max =array[0]; 79 for(int i = 0; i < array.Length; i++) 80 { 81 if (array[i] > max) max = array[i]; 82 } 83 return max; 84 } 85 86 /// <summary> 87 /// 查找数组的下标位置 88 /// </summary> 89 /// <param name="array"></param> 90 /// <param name="n"></param> 91 /// <returns></returns> 92 93 static int path(int[] array,int n) 94 { 95 int result = -1; 96 for(int i = 0; i < array.Length; i++) 97 { 98 if (n == array[i]) result = i; 99 } 100 return result; 101 } 102 103 /// <summary> 104 /// 查找字符串的下表位置 105 /// </summary> 106 /// <param name="str"></param> 107 /// <param name="c"></param> 108 /// <returns></returns> 109 110 static int searchStr(String str ,char c) 111 { 112 int result = 0; 113 char[] array = str.ToCharArray(); 114 for(int i = 0; i < array.Length; i++) 115 { 116 if (c == array[i]) 117 { 118 result++; 119 } 120 } 121 return result; 122 } 123 124 /// <summary> 125 /// 平均数 126 /// </summary> 127 /// <param name="array"></param> 128 /// <returns></returns> 129 130 static Double avg(int[] array) 131 { 132 Double count=0; 133 134 for(int i = 0; i < array.Length; i++) 135 { 136 count += array[i]; 137 } 138 139 return count / array.Length; 140 } 141 142 /// <summary> 143 /// 选择排序 144 /// </summary> 145 /// <param name="array"></param> 146 /// <returns></returns> 147 static int[] sortSelect(int[] array) 148 { 149 for(int i =0; i < array.Length - 1; i++) 150 { 151 for(int j=i+1; j < array.Length; j++) 152 { 153 if (array[i] > array[j]) 154 { 155 int temp = array[i]; 156 array[i] = array[j]; 157 array[j] = temp; 158 } 159 } 160 } 161 return array; 162 } 163 164 165 /// <summary> 166 /// 大小写字符转换 167 /// </summary> 168 /// <param name="str"></param> 169 /// <returns></returns> 170 static String toCompareChar(String str) 171 { 172 String result =""; 173 char[] array = str.ToCharArray(); 174 for(int i=0; i < array.Length; i++) 175 { 176 if(array[i]>='A' && array[i] <= 'Z') 177 { 178 result += Convert.ToChar(array[i] + 32); 179 } 180 if (array[i] >= 'a' && array[i] <= 'z') 181 { 182 result += Convert.ToChar(array[i] -32); 183 } 184 } 185 return result; 186 187 } 188 } 189 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现