(6)把分拣奇偶数的程序用泛型实现。List<int>
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
练习06namespace 分拣奇偶数泛型实现 { class Program { static void Main(string[] args) { string str = "2 7 8 3 22 9 5 11"; string [] strs = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); List<int> listeven = new List<int>(); List<int> listodd = new List<int>(); for (int i = 0; i < strs.Length; i++) { int temp = int.Parse(strs[i]); if (temp % 2 == 0) { listeven.Add(temp); } else { listodd.Add(temp); } } listodd.AddRange(listeven); foreach (int item in listodd) { Console.Write("{0} ",item); } Console.ReadKey(); } } }
(7)将int数组中的奇数放到一个新的int数组中返回。
练习07namespace int奇数分拣放到另一个int数组 { class Program { static void Main(string[] args) { int [] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int [] arr1= SelectOdd(arr); //定义一个数组接受返回的数组 foreach (int item in arr1) //对数组进行遍历输出 { Console.WriteLine(item); } Console.ReadKey(); } /// <summary> /// 输入一个数组作为参数,返回一个数组里面只有奇数 /// </summary> /// <param name="arr"></param> /// <returns></returns> public static int[] SelectOdd(int [] arr) { List<int> listodd = new List<int>(); for (int i = 0; i < arr.Length; i++) { if (arr[i] % 2 == 1) { listodd.Add(arr[i]); } } int[] arrodd = (int[])listodd.ToArray(); return arrodd; } } }
(8)从一个整数的List<int>中取出最大数(找最大值)。别用max方法。
练习08namespace 从一个整数中取出最大数 { class Program { static void Main(string[] args) { //从一个整数的List<int>中取出最大数(找最大值)。别用max方法。 int[] arr = { 53, 636, 734, 4, 5, 65, 6 }; List<int> list = new List<int>(); list.AddRange(arr); int max = 0; for (int i = 0; i < list.Count; i++) { max = max > list[i] ? max : list[i]; } Console.WriteLine(max); Console.ReadKey(); } } }
(9)把123转换为:壹贰叁。Dictionary<char,char>
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
练习09namespace 把123转换为_壹贰叁_ { class Program { static void Main(string[] args) { //把123转换为:壹贰叁。Dictionary<char,char> Dictionary<char, char> dir = new Dictionary<char, char>(); dir.Add('1', '壹'); dir.Add('2', '贰'); dir.Add('3', '叁'); string str = "123"; StringBuilder sb = new StringBuilder(); for (int i = 0; i <dir.Count; i++) { char temp = str[i]; sb.AppendFormat("{0}", dir[temp]); } Console.WriteLine(sb); Console.ReadKey(); } } }
(10) 计算字符串中每种字符出现的次数(面试题)。 “Welcome to Chinaworld”,不区分大小写,打印“W2”“e 2”“o 3”……
提示:Dictionary<char,int>,char的很多静态方法。char.IsLetter()\
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
练习10namespace 计算字符串中每种字符出现的次数 { class Program { static void Main(string[] args) { string strtemp = "Welcome to Chinaworld"; string str = strtemp.ToLower(); Dictionary<char, int> dir = new Dictionary<char, int>(); for (int i = 0; i < str.Length; i++) { if (char.IsLetter(str[i])) { if (dir.ContainsKey(str[i])) { dir[str[i]]++; } else { dir.Add(str[i], 1); } } } StringBuilder sb = new StringBuilder(); foreach(KeyValuePair<char,int> item in dir) { sb.AppendFormat("{0}\t{1}\n", item.Key, item.Value); } Console.WriteLine(sb); Console.ReadKey(); } } }