//降序排序 Console.Write("请输入数据的个数:"); int n = Convert.ToInt32(Console.ReadLine()); int []sz=new int[n]; //输入数值 for (int i = 0; i < n; i++) { Console.Write("请输入第{0}个数:",i+1); sz[i] = Convert.ToInt32(Console.ReadLine()); } //1、冒泡排序,方法易记 for (int i = 0; i < n; i++) { //依次比较相邻两个数,并按降序排列 for (int j = 1; j < n; j++) { if (sz[j] > sz[j - 1]) { int a = sz[j]; sz[j] = sz[j - 1]; sz[j - 1] = a; } } } //2、冒泡排序的精确版,计算机运行步数少 for (int i = 0; i < n - 1; i++) { //比较出最小数放入本组数末位,下次循环比较剩余的数 for (int j = 1; j < n - 1 - i; j++) { if (sz[j] > sz[j - 1]) { int a = sz[j]; sz[j] = sz[j - 1]; sz[j - 1] = a; } } } //3、逐位排序 for (int i = 0; i < n; i++) { //本组数字的首位往下逐位比较,取出最大值放入首位 for (int j = i + 1; j < n; j++) { if (sz[i] < sz[j]) { int a = sz[i]; sz[i] = sz[j]; sz[j] = a; } } } //逐位输出 for (int i = 0; i < n; i++) { Console.WriteLine(sz[i]); }
//选出2个最高分、2个最低分 Console.Write("请输入数据个数:"); int n = Convert.ToInt32(Console.ReadLine()); int[] fenshu = new int[n]; //输入分数 for (int i = 0; i < n; i++) { Console.Write("请输入第{0}个分数:", i + 1); fenshu[i] = Convert.ToInt32(Console.ReadLine()); } //选出2个最高分,2个最低分 int max1 = fenshu[0], max2 = fenshu[1]; //定义最高分、次高分,并赋值 int min1 = fenshu[0], min2 = fenshu[1]; //定义最低分、次低分,并赋值 for (int i = 1; i < n; i++) { if (max1 <= fenshu[i]) //取第三个值,比较三个数大小,扔掉最小值 { max2 = max1; max1=fenshu[i]; } else if (max2 < fenshu[i]) { max2=fenshu[i]; } if (min1 >= fenshu[i]) //取第三个值,比较三个数大小,扔掉最大值 { min2 = min1; min1 = fenshu[i]; } else if (min2 > fenshu[i]) { min2 = fenshu[i]; } } //输出 Console.WriteLine("两个最高分是{0},{1};两个最低分是{2},{3}:",max1,max2,min1,min2);
//手机号抽奖 Console.Write("请输入手机号个数:"); int n = Convert.ToInt32(Console.ReadLine()); string[] phone_no = new string[n]; for (int i = 0; i < n; i++) { Console.Write("请输入第{0}个手机号:", i + 1); phone_no[i] = Console.ReadLine(); } DateTime over = DateTime.Now.AddSeconds(5); //定义一个结束时间内,为5秒后 Random a = new Random(); //定义一个随机数生成器 Console.ForegroundColor = ConsoleColor.Blue; //前景色设为蓝色 while (DateTime.Now <= over) { int b = a.Next(n); Console.Clear(); //清屏 Console.WriteLine(phone_no[b]); } //以下为抽奖作弊设置 Console.Clear(); Console.WriteLine("13999999999");