コード例 #1
0
        static void Main(string[] args)
        {
            Console.Title = "将学生按成绩从低到高排列";
            StudentList students = new StudentList(20);
            Console.WriteLine("排序前学生信息表:");
            for (int i = 0; i < 20; i++)
            {
                Console.WriteLine("学号:{0}\t姓名:{1}\t成绩:{2}", students[i].Number, students[i].Name, students[i].Result);
            }

            Student temp;

            //将建堆后的根结点与索引为i的结点交换
            for (int i = 19; i >=1; i--)
            {
            CreateHeap(students, i);
            temp = students[0];
            students[0] = students[i];
            students[i] = temp;
            }

            Console.ReadLine();
            Console.Clear();
            Console.WriteLine("排序后学生信息表:");
            for (int i = 0; i < 20; i++)
            {
                Console.WriteLine("学号:{0}\t姓名:{1}\t成绩:{2}",
                    students[i].Number, students[i].Name, students[i].Result);
            }

            Console.ReadLine();
        }
コード例 #2
0
        static void Main(string[] args)
        {
            Console.Title = "查找指定姓名的学生信息";

            Console.WriteLine("学生名单如下:");
            StudentList students = new StudentList(20);
            for (int i = 0; i < 20; i++)
            {
            Console.Write(students[i].Name + "\t");
            if ((i + 1) % 5 == 0) Console.WriteLine();
            }
            string name;
            do
            {

            Console.WriteLine("请输入要查询的学生姓名:");
            name = Console.ReadLine();
            int i = 0, count = 0;
            //顺序查找学生指定学生信息
            for (; i < 20; i++)
            {
            if (students[i].Name == name)
            {
            Console.WriteLine("学号:{0}\t年级:{1}\t成绩:{2}",
            students[i].Number, students[i].Grade, students[i].Result);
            count++;
            }
            }
            if (count == 0) Console.WriteLine("无此学生信息!");

            } while (name != string.Empty);

            Console.ReadLine();
        }
コード例 #3
0
        static void Main(string[] args)
        {
            Console.Title = "求学生成绩在前十名的学生";
            StudentList students = new StudentList(20);
            Console.WriteLine("排序前学生信息表:");
            for (int i = 0; i < 20; i++)
            {
                Console.WriteLine("学号:{0}\t姓名:{1}\t成绩:{2}", students[i].Number, students[i].Name, students[i].Result);
            }
            Console.ReadLine();

            Student temp;
            int max = 0;
            for (int i = 0; i < 10; i++)
            {
            max = i;
            for (int j = i + 1; j < 20; j++)
            {
            if (students[j].Result > students[max].Result) max = j;//求成绩最好的学生索引
            }
            //将成绩最好的学生与索引i的学生交换
            temp = students[i];
            students[i] = students[max];
            students[max] = temp;
            }

            Console.Clear();
            Console.WriteLine("成绩排在前十名的学生信息表:");
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("学号:{0}\t姓名:{1}\t成绩:{2}", students[i].Number, students[i].Name, students[i].Result);
            }

            Console.ReadLine();
        }
コード例 #4
0
 //将sts1[begin..end]归并排序为sts2[begin..end]
 static void MergeSort(StudentList sts1, Student[] sts2, int begin, int end)
 {
     Student[] sts3 = new Student[sts1.Length];
     if (begin == end)
     sts2[begin] = sts1[begin];                      //如果只有一个元素则直接将其归并到sts2中
     else
     {
     int divide = (begin + end) / 2;                 //将表分割成两部分
     MergeSort(sts1, sts3, begin, divide);           //对左半部分进行归并排序并存储到sts3中
     MergeSort(sts1, sts3, divide + 1, end);         //对右半部分进行归并排序并存储到sts3中
     Merge(sts3, sts2, begin, divide, end);          //将左右两部分归并到sts2中
     }
 }
コード例 #5
0
        static void Main(string[] args)
        {
            Console.Title = "查询具有指定成绩的学生信息";
            StudentList students = new StudentList(20);

            Console.WriteLine("学生成绩单如下:");
            for (int i = 0; i < 20; i++)
            {
                Console.Write(students[i].Result + "\t");
                if ((i + 1) % 5 == 0) Console.WriteLine();
            }

            BTreeNode root = new BTreeNode(students[0]);            //创建二叉排序树根结点
            for (int i = 1; i < 20; i++)
            {
            CreateBinarySortingTree(root, students[i]);         //根据学生成绩创建二叉排序树
            }
            int result;
            //在二叉排序树上的查找指定成绩学生信息

            do
            {
            Console.WriteLine("请输入要查询的学生的成绩:");
            if (!int.TryParse(Console.ReadLine(), out result)) result = -1;
            BTreeNode node = root;
            bool successful = false;
            while (node != null)
            {
            if (result > node.Student.Result) node = node.RightChild;//查找右子树
            else if (result < node.Student.Result) node = node.LeftChild;//查找左子树
            else//查找成功
            {
            Console.WriteLine("学号:{0}\t姓名:{1}\t年级:{2}",
            node.Student.Number, node.Student.Name, node.Student.Grade);
            successful = true;
            break;
            }
            }
            if (!successful) Console.WriteLine("无此学生信息!");//查找失败

            } while (result != -1);

            Console.ReadLine();
        }
コード例 #6
0
        static void Main(string[] args)
        {
            Console.Title = "按学生成绩构造哈希表";
            StudentList students = new StudentList(20);

            Console.WriteLine("学生成绩单如下:");
            for (int i = 0; i < 20; i++)
            {
                Console.Write(students[i].Result + "\t");
                if ((i + 1) % 5 == 0) Console.WriteLine();
            }

            //创建哈希表
            StudentNode[] hashTable = new StudentNode[5];
            for (int i = 0; i < 20; i++)
            {
            int index = students[i].Result % 5;                 //计算哈希地址
            if (hashTable[index] == null) hashTable[index] = new StudentNode(students[i], null);
            else hashTable[index] = new StudentNode(students[i], hashTable[index]);
            }

            int result;
            do
            {
            Console.WriteLine("请输入要查询的学生的成绩:");
            if (!int.TryParse(Console.ReadLine(), out result)) break;
            bool successful = false;                                //标记查找是否成功
            StudentNode node = hashTable[result % 5];               //计算哈希地址
            while (node != null)
            {
            if (node.Student.Result == result)                  //查找成功
            {
            Console.WriteLine("学号:{0}\t姓名:{1}\t年级:{2}",
            node.Student.Number, node.Student.Name, node.Student.Grade);
            successful = true;
            }
            node = node.Next;
            }
            if (!successful) Console.WriteLine("无此学生信息!");      //查找失败
            } while (result != -1);

            Console.ReadLine();
        }
コード例 #7
0
        static void Main(string[] args)
        {
            Console.Title = "将学生按成绩进行希尔排序";

            StudentList students = new StudentList(20);
            Console.WriteLine("排序前的学生信息表:");
            for (int i = 0; i < 20; i++)
            {
            Console.WriteLine("学号:{0}\t姓名:{1}\t成绩:{2}",
            students[i].Number, students[i].Name, students[i].Result);
            }

            for (int increment = 5; increment > 0; increment--)
            {
            //遍历索引从increment开始的学生信息
            for (int i = increment; i < 20; i++)
            {
            //如果索引为i的学生成绩大于索引为i-increment的学生成绩
            //则前i/increment+1个学生成绩已经有序
            if (students[i].Result > students[i - increment].Result)
            {
            Student temp = students[i];
            int j = i - increment;
            //将索引为i的学生信息插入到其所在分组中合适的位置
            for (; j >= 0 && temp.Result > students[j].Result; j -= increment)
            {
                students[j + increment] = students[j];
            }
            students[j + increment] = temp;
            }
            }
            }

            Console.ReadLine();
            Console.Clear();
            Console.WriteLine("排序后的学生信息表:");
            for (int i = 0; i < 20; i++)
            {
                Console.WriteLine("学号:{0}\t姓名:{1}\t成绩:{2}", students[i].Number, students[i].Name, students[i].Result);
            }

            Console.ReadLine();
        }
コード例 #8
0
 //建立大根堆
 static void CreateHeap(StudentList students, int n)
 {
     //从第一个非叶子结点开始遍历
     for (int i = (n -1) / 2; i >= 0; i--)
     {
     int lChild = i * 2 + 1;                 //左孩子索引
     int rChild = i * 2 + 2;                 //右孩子索引
     int max = i;                            //最大值索引
     //将结点i的学生成绩与其左孩子结点的学生成绩比较
     if (students[lChild].Result > students[max].Result) max = lChild;
     //将结点i的学生成绩与其右孩子结点的学生成绩比较
     if (rChild <= n && students[rChild].Result > students[max].Result) max = rChild;
     //将成绩最好的结点调整到索引为i的结点上
     if (max != i)
     {
     Student temp = students[i];
     students[i] = students[max];
     students[max] = temp;
     }
     }
 }
コード例 #9
0
        static void Main(string[] args)
        {
            Console.Title = "查找指定学号的学生信息";

            Console.WriteLine("学生学号列表如下:");
            StudentList students = new StudentList(20);
            for (int i = 0; i < 20; i++)
            {
            Console.Write(students[i].Number + "\t");
            if ((i + 1) % 5 == 0) Console.WriteLine();
            }
            int number;
            do
            {
            Console.WriteLine("请输入要查询的学生学号:");
            if (!int.TryParse(Console.ReadLine(), out number)) number = 0;
            int middle = 0;                                     //用于查找的对象索引
            int low = 0;                                        //查找区域索引下限
            int high = 19;                                      //查找区域索引上限
            int flag = -1;                      //用于标记查找成功时的索引,如果查找失败则为-1
            //折半查找学生指定学生信息
            while(low <= high)
            {
            middle = (low + high) / 2;
            if (number > students[middle].Number) low = middle + 1;//下一次在右半区域中查找
            else if (number < students[middle].Number) high = middle - 1;//下一次在左半区域中查找
            else
            {
            flag = middle;//查找成功,将记录索引存入flag中
            break;          //跳出查找循环
            }
            }
            if (flag > 0)//查找成功
            Console.WriteLine("姓名:{0}\t年级:{1}\t成绩:{2}",
            students[flag].Name, students[flag].Grade, students[flag].Result);
            else Console.WriteLine("无此学生信息!");//查找失败
            } while (number != 0);

            Console.ReadLine();
        }
コード例 #10
0
        static void Main(string[] args)
        {
            Console.Title = "对学生表的前十位学生按成绩排序";

            StudentList students = new StudentList(20);
            Console.WriteLine("排序前的学生信息表:");
            for (int i = 0; i < 20; i++)
            {
            Console.WriteLine("学号:{0}\t姓名:{1}\t成绩:{2}",
            students[i].Number, students[i].Name, students[i].Result);
            }

            //遍历索引从1开始的学生信息
            for (int i = 1; i < 10; i++)
            {
            //如果索引为i的学生成绩大于索引为i-1的学生成绩,则前i+1个学生成绩已经有序
            if (students[i].Result > students[i - 1].Result)
            {
            Student temp = students[i];
            int j = i - 1;
            //将索引为i的学生信息插入到其合适的位置
            for (; j >= 0 && temp.Result > students[j].Result; j--)
            {
            students[j + 1] = students[j];
            }
            students[j + 1] = temp;
            }
            }
            Console.ReadLine();
            Console.Clear();
            Console.WriteLine("排序后的学生信息表:");
            for (int i = 0; i < 20; i++)
            {
            Console.WriteLine("学号:{0}\t姓名:{1}\t成绩:{2}", students[i].Number, students[i].Name, students[i].Result);
            }

            Console.ReadLine();
        }
コード例 #11
0
        static void Main(string[] args)
        {
            Console.Title = "对学生表的前十位学生按成绩排序";

            StudentList students = new StudentList(20);
            Console.WriteLine("学生信息表:");
            for (int i = 0; i < 20; i++)
            {
                Console.WriteLine("学号:{0}\t姓名:{1}\t成绩:{2}", students[i].Number, students[i].Name, students[i].Result);
            }
            //正向冒泡排序求最小值
            for (int i = 1; i < 20; i++)
            {
            //成绩差的上浮
            if (students[i].Result > students[i - 1].Result)
            {
            Student temp = students[i];
            students[i] = students[i - 1];
            students[i - 1] = temp;
            }
            }
            //逆向冒泡排序求最大值
            for (int i = 19; i > 0; i--)
            {
            //成绩好的下沉
            if (students[i].Result > students[i - 1].Result)
            {
            Student temp = students[i];
            students[i] = students[i - 1];
            students[i - 1] = temp;
            }
            }
            Console.WriteLine("最好成绩为:{0} 最差成绩为:{1}", students[0].Result, students[19].Result);

            Console.ReadLine();
        }
コード例 #12
0
        static void Main(string[] args)
        {
            Console.Title = "将学生按成绩进行归并排序";

            StudentList students = new StudentList(20);
            Student[] studentsSort = new Student[20];
            Console.WriteLine("排序前的学生信息表:");
            for (int i = 0; i < 20; i++)
            {
                Console.WriteLine("学号:{0}\t姓名:{1}\t成绩:{2}", students[i].Number, students[i].Name, students[i].Result);
            }
            Console.ReadLine();

            MergeSort(students, studentsSort, 0, 19);

            Console.Clear();
            Console.WriteLine("排序后的学生信息表:");
            for (int i = 0; i < 20; i++)
            {
                Console.WriteLine("学号:{0}\t姓名:{1}\t成绩:{2}", studentsSort[i].Number, studentsSort[i].Name, studentsSort[i].Result);
            }

            Console.ReadLine();
        }
コード例 #13
0
        static void Main(string[] args)
        {
            Console.Title = "求成绩合格的学生";

            StudentList students = new StudentList(20);
            Console.WriteLine("排序前学生信息表:");
            for (int i = 0; i < 20; i++)
            {
                Console.WriteLine("学号:{0}\t姓名:{1}\t成绩:{2}", students[i].Number, students[i].Name, students[i].Result);
            }

            Console.ReadLine();

            int low = 0, high = 19;
            int result = 60;                                        //合格的成绩
            Student temp = students[low];
            //第一趟快速排序,将学生按成绩分为大于等于60的和小于60的两部分
            while (low < high)
            {
            while (low < high && students[high].Result >= result) high--;
            students[low++] = students[high];
            while (low < high && students[low].Result <= result) low++;
            students[high--] = students[low];
            }
            students[low] = temp;
            if (temp.Result < result) low++;

            Console.Clear();
            Console.WriteLine("成绩合格的学生信息表:");
            for (int i = low; i < 20; i++)
            {
                Console.WriteLine("学号:{0}\t姓名:{1}\t成绩:{2}", students[i].Number, students[i].Name, students[i].Result);
            }

            Console.ReadLine();
        }
コード例 #14
0
        static void Main(string[] args)
        {
            Console.Title = "将学生信息表顺序倒置";

            StudentList students = new StudentList(10);             //创建含有10个元素的学生信息表
            Console.WriteLine("按顺序输出学生姓名:");
            for (int i = 0; i < 10; i++) Console.Write(students[i].Name + "\t");
            //将学生表前面的元素与后面的元素互换
            for (int i = 0; i < 5; i++)
            {
            Student temp = students[i];
            students[i] = students[9 - i];
            students[9 - i] = temp;
            }
            Console.WriteLine("输出学生信息表倒置后的学生姓名:");
            for (int i = 0; i < 10; i++) Console.Write(students[i].Name + "\t");

            Console.Read();
        }