C# 小例子
Posted 西枫叶落
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# 小例子相关的知识,希望对你有一定的参考价值。
数组冒泡排序(升序排序):
1 int[] num = { 8, 5, 7, 6, 54, 6, 4, 12, 2, 0, 1, 3 }; 2 //第一种 3 for (int i = 0; i < num.Length - 1; i++) 4 { 5 for (int j = 0; j < num.Length - i - 1; j++) 6 { 7 if (num[j] > num[j + 1]) 8 { 9 int temp = num[j]; 10 num[j] = num[j + 1]; 11 num[j + 1] = temp; 12 } 13 } 14 } 15 //第二种 16 for (int i = 0; i < num.Length - 1; i++) 17 { 18 for (int j = 0; j < num.Length - 1; j++) 19 { 20 if (num[j] > num[j + 1]) 21 { 22 int temp = num[j]; 23 num[j] = num[j + 1]; 24 num[j + 1] = temp; 25 } 26 } 27 } 28 //第三种 29 for (int i = 0; i < num.Length - 1; i++) 30 { 31 for (int j = 0; j < num.Length - 1 - i; j++) 32 { 33 if (num[j] > num[j + 1]) 34 { 35 int temp = num[j]; 36 num[j] = num[j + 1]; 37 num[j + 1] = temp; 38 } 39 } 40 } 41 //第四种: 42 for (int i = 0; i < num.Length - 1; i++) 43 { 44 for (int j = i + 1; j < num.Length; j++) 45 { 46 if (num[i] > num[j]) 47 { 48 int temp = num[i]; 49 num[i] = num[j]; 50 num[j] = temp; 51 } 52 } 53 }
C#中有提供专门排序的方法:System.Array.Sort(要排序的数组);//升序排序
升序排序可以先降序后再反转排序 。
数组降序排序:
1 int[] num = { 8, 5, 7, 6, 54, 6, 4, 12, 2, 0, 1, 3 }; 2 //第一种 3 for (int i = 0; i < num.Length - 1; i++) 4 { 5 for (int j = 0; j < num.Length - i - 1; j++) 6 { 7 if (num[j] < num[j + 1]) 8 { 9 int temp = num[j]; 10 num[j] = num[j + 1]; 11 num[j + 1] = temp; 12 } 13 } 14 } 15 //第二种 16 for (int i = 0; i < num.Length - 1; i++) 17 { 18 for (int j = 0; j < num.Length - 1; j++) 19 { 20 if (num[j] < num[j + 1]) 21 { 22 int temp = num[j]; 23 num[j] = num[j + 1]; 24 num[j + 1] = temp; 25 } 26 } 27 } 28 //第三种 29 for (int i = 0; i < num.Length - 1; i++) 30 { 31 for (int j = 0; j < num.Length - 1 - i; j++) 32 { 33 if (num[j] < num[j + 1]) 34 { 35 int temp = num[j]; 36 num[j] = num[j + 1]; 37 num[j + 1] = temp; 38 } 39 } 40 } 41 //第四种: 42 for (int i = 0; i < num.Length - 1; i++) 43 { 44 for (int j = i + 1; j < num.Length; j++) 45 { 46 if (num[i] < num[j]) 47 { 48 int temp = num[i]; 49 num[i] = num[j]; 50 num[j] = temp; 51 } 52 } 53 }
可用C#中中提供的专门排序的方法来降序,降序排序可以先Array.Sort() 再 Array.Reverse()。【先升序再反转过来】
降序排序可以先升序后再反转排序 。
数组反转排序:
1 string[] str = { "a", "b", "c", "d", "e", "f", "g" }; 2 for (int i = 0; i < str.Length / 2; i++) 3 { 4 string temp = str[i]; 5 str[i] = str[str.Length - 1 - i]; 6 str[str.Length - 1 - i] = temp; 7 }
C#中有提供专门排序的方法:System.Array.Reverse(要排序的数组);//颠倒排序,反转排序
输入一个年份和月份输出这个月份的天数:
1 Console.WriteLine("请输入年份"); 2 try 3 { 4 int year = Convert.ToInt32(Console.ReadLine()); 5 Console.WriteLine("请输入月份"); 6 try 7 { 8 int month = Convert.ToInt32(Console.ReadLine()); 9 if (month >= 1 && month <= 12) 10 { 11 int day = 0; 12 switch (month) 13 { 14 case 1: 15 case 3: 16 case 5: 17 case 7: 18 case 8: 19 case 10: 20 case 12: day = 31; break; 21 case 2: if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) day = 29; else day = 28; break; 22 default: day = 31; break; 23 } 24 Console.WriteLine("{0}年{1}月有{2}天", year, month, day); 25 } 26 else { Console.WriteLine("输入的月份超出范围"); } 27 } 28 catch { Console.WriteLine("输入的月份有误,程序退出"); } 29 } 30 catch { Console.WriteLine("输入的年份有误,程序退出"); } 31 Console.ReadKey();
不断要求用户输入姓名,输入q就结束:
1 string strname = ""; 2 3 while (strname != "q") 4 { 5 Console.WriteLine("请输入你的姓名,输入q结束"); 6 strname = Console.ReadLine(); 7 } 8 Console.ReadKey(); 9 //do...while 10 /*do 11 { 12 Console.WriteLine("请输入你的姓名,输入q结束"); 13 strname = Console.ReadLine(); 14 } while (strname != "q"); 15 Console.ReadKey();*/
不断要求输入一个数字(假定用户输入的是正整数),当用户输入end时显示刚才输入的数字中的最大值:
1 string input = ""; 2 int max = 0; 3 while (input != "end") 4 { 5 Console.WriteLine("请输入一个数字,输入end将显示输入数中的最大值"); 6 input = Console.ReadLine(); 7 if (input != "end") 8 { 9 try 10 { 11 int number = Convert.ToInt32(input); 12 if (number > max) 13 { 14 max = number; 15 } 16 } 17 catch 18 { 19 Console.WriteLine("您收收入的字符串有误,请重新输入"); 20 } 21 } 22 else 23 { 24 Console.WriteLine("您刚才输入的数字中最大值为{0}", max); 25 } 26 } Console.ReadKey();
九九乘法表:
1 for (int i = 1; i <= 9; i++) 2 { 3 for (int j = 1; j <= i; j++) 4 { 5 Console.Write("{0}*{1}={2}\\t", i, j, i * j); 6 } Console.WriteLine(); 7 } Console.ReadKey();
百钱买百鸡:(公鸡5元/只,母鸡3元/只,小鸡3只/元)
1 //方法 2 3 /// <summary> 4 /// 求百钱买百鸡的购买方案,返回三种购买方案。 5 /// </summary> 6 /// <param name="Num1">返回的包含第一种购买方案的输出型数组,数组下标从0开始,依次为公鸡数量、母鸡数量、小鸡数量</param> 7 /// <param name="Num2">返回的包含第二种购买方案的输出型数组,数组下标从0开始,依次为公鸡数量、母鸡数量、小鸡数量</param> 8 /// <param name="Num3">返回的包含第三种购买方案的输出型数组,数组下标从0开始,依次为公鸡数量、母鸡数量、小鸡数量</param> 9 public static void BuyChicken(out int[] Num1, out int[] Num2, out int[] Num3) 10 { 11 //g5,m3,x(1/3) 12 Num1 = new int[3]; 13 Num2 = new int[3]; 14 Num3 = new int[3]; 15 //int g, m, x; 16 string str = ""; 17 for (int Cock = 1; Cock <= 19; Cock++) 18 { 19 for (int Hen = 1; Hen <= 33; Hen++) 20 { 21 int Chicken = 100 - Cock - Hen; 22 if (Cock * 5 + Hen * 3 + Chicken / 3 == 100 && Chicken % 3 == 0) 23 { 24 str += Cock.ToString() + "|" + Hen.ToString() + "|" + Chicken.ToString() + "|"; 25 } 26 } 27 } 28 string[] strArray = str.Split(new char[] { \'|\' }); 29 for (int i = 0; i < strArray.Length; i++) 30 { 31 if (i >= 0 && i <= 2) 32 Num1[i] = Convert.ToInt32(strArray[i]); 33 if (i >= 3 && i <= 5) 34 Num2[i - 3] = Convert.ToInt32(strArray[i]); 35 if (i >= 6 && i <= 8) 36 Num3[i - 6] = Convert.ToInt32(strArray[i]); 37 } 38 } 39 40 //调用 41 int[] Num1; 42 int[] Num2; 43 int[] Num3; 44 BuyChicken(out Num1, out Num2, out Num3); 45 Console.WriteLine("公鸡数量:{0},母鸡数量:{1},小鸡数量:{2}", Num1[0], Num1[1], Num1[2]); 46 Console.WriteLine("公鸡数量:{0},母鸡数量:{1},小鸡数量:{2}", Num2[0], Num2[1], Num2[2]); 47 Console.WriteLine("公鸡数量:{0},母鸡数量:{1},小鸡数量:{2}", Num3[0], Num3[1], Num3[2]);
1 for (int Cock = 1; Cock <= 19; Cock++) 2 { 3 for (int Hen = 1; Hen <= 33; Hen++) 4 { 5 int Chicken = 100 - Cock - Hen; 6 if (Cock * 5 + Hen * 3 + Chicken / 3 == 100 && Chicken % 3 == 0) 7 { 8 Console.WriteLine("Cock:{0},Hen:{1},Chicken:{2}", Cock, Hen, Chicken); 9 } 10 } 11 }
求水仙花数:
1 /// <summary> 2 /// 求水仙花数 3 /// </summary> 4 /// <returns>返回的包含水仙花数的数组</returns> 5 public static string[] SxhNum() 6 { 7 int a, b, c; 8 string str = null; 9 for (int num = 100; num < 1000; num++) 10 { 11 a = num / 100;//1 12 b = num / 10 % 10;//5 13 c = num % 10;//3 14 if (Math.Pow(a, 3) + Math.Pow(b, 3) + Math.Pow(c, 3) == num) 15 { 16 str += num.ToString() + "|"; 17 } 18 } 19 return str.Substring(0, str.Length - 1).Split(new char[] { \'|\' }); 20 } 21 22 //调用 23 string[] str = SxhNum(); 24 for (int i = 0; i < str.Length; i++) 25 { 26 Console.WriteLine("水仙花数:" + str[i]); 27 }