冒泡排序和几个题目
利用冒泡排序将 6, 3, 2, 1, 7, 9, 5从小到大排列
1 int[] a = new int[] { 6, 3, 2, 1, 7, 9, 5 }; 2 for (int i = 1; i < a.Length - 1; i++) //趟数 3 { 4 for (int j = 0; j < a.Length - 1; j++) //每趟的次数 5 { 6 int zhong; 7 if (a[j] > a[j + 1]) 8 { 9 zhong = a[j]; 10 a[j] = a[j + 1]; 11 a[j + 1] = zhong; 12 } 13 } 14 } 15 foreach (int t in a)//遍历数组 16 { 17 Console.WriteLine(t); 18 }
输入人数,输入每个人的成绩,需要最高分 最低分 去掉两个最高分 去掉两个最低分 求平均分
for (; ; ) { Console.Write("请输入人数:"); int a = int.Parse(Console.ReadLine()); double[] b = new double[a]; if (a >= 5) { for (int i = 0; i < a; i++) //给数组的每个数赋值 { Console.Write("请输入第" + (i + 1) + "个人的成绩:"); b[i] = double.Parse(Console.ReadLine()); } for (int i = 1; i < b.Length - 1; i++) //利用冒泡排序排列成绩 { double zhong; for (int j = 0; j < b.Length - 1; j++) { if (b[j] > b[j + 1]) { zhong = b[j]; b[j] = b[j + 1]; b[j + 1] = zhong; } } } Console.WriteLine("最高分为:" + b[b.Length - 1] + "最低分为:" + b[0]); double sum = 0; for (int i = 2; i <= (b.Length - 3); i++) { sum += b[i]; } Console.WriteLine("平均分为:" + sum / (a - 4)); break; } else { Console.WriteLine("您的输入有误!"); } }
输入n个手机号,然后随机滚动,五秒后抽取一个号码:
1 Console.Write("请输入手机号的个数n:"); 2 int n = int.Parse(Console.ReadLine()); 3 string[] m = new string[n]; 4 for (int i = 0; i < n; i++) 5 { 6 Console.Write("请输入第" + (i + 1) + "个人的手机号:"); 7 m[i] = Console.ReadLine(); 8 } 9 Console.WriteLine("号码输入完毕,按回车滚动!"); 10 Console.ReadLine(); 11 Random ran = new Random(); 12 for (int i = 0; ; i++) 13 { 14 Console.Clear(); 15 Console.WriteLine(m[ran.Next(n)]); 16 System.Threading.Thread.Sleep(100); 17 if (i == 49) 18 { 19 break; 20 } 21 22 }