
排序
小·幸·运
你的所有努力最后都会回赠予你。
展开
-
LeetCode 215. 数组中的第K个最大元素
数组中的第K个最大元素 在未排序的数组中找到第 k 个最大的元素。请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。 示例 1: 输入: [3,2,1,5,6,4] 和 k = 2 输出: 5 示例 2: 输入: [3,2,3,1,2,4,5,5,6] 和 k = 4 输出: 4 说明: 你可以假设 k 总是有效的,且 1 ≤ k ≤ 数组的长度。 方法一:使用堆排序 建立大顶堆,每次建堆完毕,把堆顶元素和数组最后面的数调换位置,如此重复k次。 参考代码: class S.原创 2021-03-27 14:42:50 · 168 阅读 · 0 评论 -
Python实现各种排序算法
一、快速排序 快速排序使用分治法(Divide and conquer)策略来把一个序列(list)分为较小和较大的2个子序列,然后递归地排序两个子序列。 步骤为: 挑选基准值:从数列中挑出一个元素,称为"基准"(pivot); 分割:重新排序数列,所有比基准值小的元素摆放在基准前面,所有比基准值大的元素摆在基准后面(与基准值相等的数可以到任何一边)。在这个分割结束之后,对基准值的排序就已经完成; 递归排序子序列:递归地将小于基准值元素的子序列和大于基准值元素的子序列排序。 递归到最底部的判断条件是数列的原创 2021-03-27 12:58:55 · 513 阅读 · 2 评论 -
PAT乙级B1070 结绳 (25 分)
给定一段一段的绳子,你需要把它们串成一条绳。每次串连的时候,是把两段绳子对折,再如下图所示套接在一起。这样得到的绳子又被当成是另一段绳子,可以再次对折去跟另一段绳子串连。每次串连后,原来两段绳子的长度就会减半。 给定 N 段绳子的长度,你需要找出它们能串成的绳子的最大长度。 输入格式: 每个输入包含 1 个测试用例。每个测试用例第 1 行给出正整数 N (2≤N≤104);第 2 ...原创 2019-03-06 15:21:25 · 172 阅读 · 0 评论 -
PAT甲级A1083 List Grades (25 分)
Given a list of N student records with name, ID and grade. You are supposed to sort the records with respect to the grade in non-increasing order, and output those student records of which the grades ...原创 2019-02-20 20:26:51 · 115 阅读 · 0 评论 -
PAT甲级A1114 Family Property (25 分)
This time, you are supposed to help us collect the data for family-owned property. Given each person's family members, and the estate(房产)info under his/her own name, we need to know the size of each f...原创 2019-03-01 15:09:59 · 249 阅读 · 0 评论 -
PAT甲级A1101 Quick Sort (25 分)
There is a classical process named partition in the famous quick sort algorithm. In this process we typically choose one element as the pivot. Then the elements less than the pivot are moved to its le...原创 2019-02-25 18:50:38 · 206 阅读 · 0 评论 -
PAT甲级A1075 PAT Judge (25 分)
The ranklist of PAT is generated from the status list, which shows the scores of the submissions. This time you are supposed to generate the ranklist for PAT. Input Specification: Each input file co...原创 2019-02-14 15:16:02 · 233 阅读 · 0 评论 -
PAT甲级A1080 Graduate Admission (30 分)
It is said that in 2011, there are about 100 graduate schools ready to proceed over 40,000 applications in Zhejiang Province. It would help a lot if you could write a program to automate the admission...原创 2019-02-19 20:09:13 · 206 阅读 · 0 评论 -
PAT甲级A1129 Recommendation System (25 分)
Recommendation system predicts the preference that a user would give to an item. Now you are asked to program a very simple recommendation system that rates the user's preference by the number of time...原创 2019-03-09 16:39:20 · 208 阅读 · 0 评论 -
PAT乙级B1055 集体照 (25 分)
拍集体照时队形很重要,这里对给定的 N 个人 K 排的队形设计排队规则如下: 每排人数为 N/K(向下取整),多出来的人全部站在最后一排; 后排所有人的个子都不比前排任何人矮; 每排中最高者站中间(中间位置为 m/2+1,其中 m 为该排人数,除法向下取整); 每排其他人以中间人为轴,按身高非增序,先右后左交替入队站在中间人的两侧(例如5人身高为190、188、...原创 2019-03-04 14:01:53 · 141 阅读 · 0 评论 -
PAT甲级A1153 Decode Registration Card of PAT (25 分)
A registration card number of PAT consists of 4 parts: the 1st letter represents the test level, namely, T for the top level, A for advance and B for basic; the 2nd - 4th digits are the test site nu...原创 2019-03-15 21:10:08 · 290 阅读 · 0 评论 -
PAT乙级B1080 MOOC期终成绩 (25 分)
对于在中国大学MOOC(http://www.icourse163.org/ )学习“数据结构”课程的学生,想要获得一张合格证书,必须首先获得不少于200分的在线编程作业分,然后总评获得不少于60分(满分100)。总评成绩的计算公式为 G=(Gmid−term×40%+Gfinal×60%)G = (G_{mid-term}\times 40\% + G_{final}\times 60\%)G=...原创 2019-03-11 20:51:04 · 424 阅读 · 0 评论 -
PAT甲级A1137 Final Grading (25 分)
For a student taking the online course "Data Structures" on China University MOOC (http://www.icourse163.org/), to be qualified for a certificate, he/she must first obtain no less than 200 points from...原创 2019-03-11 20:56:19 · 214 阅读 · 0 评论 -
PAT乙级B1095 解码PAT准考证 (25 分)
PAT 准考证号由 4 部分组成: 第 1 位是级别,即 T 代表顶级;A 代表甲级;B 代表乙级; 第 2~4 位是考场编号,范围从 101 到 999; 第 5~10 位是考试日期,格式为年、月、日顺次各占 2 位; 最后 11~13 位是考生编号,范围从 000 到 999。 现给定一系列考生的准考证号和他们的成绩,请你按照要求输出各种统计信息。 输入格式: 输入首先在一行中给出...原创 2019-03-16 13:49:21 · 514 阅读 · 0 评论 -
PAT乙级B1085 PAT单位排行 (25 分)
每次 PAT 考试结束后,考试中心都会发布一个考生单位排行榜。本题就请你实现这个功能。 输入格式: 输入第一行给出一个正整数 N(≤105),即考生人数。随后 N 行,每行按下列格式给出一个考生的信息: 准考证号 得分 学校 其中准考证号是由 6 个字符组成的字符串,其首字母表示考试的级别:B代表乙级,A代表甲级,T代表顶级;得分是 [0, 100] 区间内的整数;学校是由不超过...原创 2019-03-12 16:38:31 · 279 阅读 · 0 评论 -
PAT甲级A1141 PAT Ranking of Institutions (25 分)
After each PAT, the PAT Center will announce the ranking of institutions based on their students' performances. Now you are asked to generate the ranklist. Input Specification: Each input file conta...原创 2019-03-12 16:59:47 · 308 阅读 · 0 评论 -
PAT甲级A1098 Insertion or Heap Sort (25 分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data,...原创 2019-02-24 19:15:15 · 261 阅读 · 0 评论 -
PAT甲级A1109 Group Photo (25 分)
Formation is very important when taking a group photo. Given the rules of forming K rows with N people as the following: The number of people in each row must be N/K (round down to the nearest inte...原创 2019-02-27 17:28:36 · 282 阅读 · 0 评论 -
PAT甲级A1089 Insert or Merge (25 分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data,...原创 2019-02-22 17:44:16 · 299 阅读 · 0 评论 -
PAT乙级B1020 月饼 (25)
月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼。现给定所有种类月饼的库存量、总售价、以及市场的最大需求量,请你计算可以获得的最大收益是多少。注意:销售时允许取出一部分库存。样例给出的情形是这样的:假如我们有3种月饼,其库存量分别为18、15、10万吨,总售价分别为75、72、45亿元。如果市场的最大需求量只有20万吨,那么我们最大收益策略应该是卖出全部15万吨第2种月饼、以...原创 2018-05-23 21:47:30 · 146 阅读 · 0 评论 -
LeetCode--628. Maximum Product of Three Numbers
Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3] Output: 6 Example 2: Input: [1,2,3,4] Output: 24 Note: The l...原创 2018-06-04 20:15:21 · 174 阅读 · 0 评论 -
PAT甲级A1017 Queueing at Bank (25)
Suppose a bank has K windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. All the customers have to wait in line behind the yellow li...原创 2018-08-03 21:32:14 · 176 阅读 · 0 评论 -
PAT甲级A1012 The Best Rank (25)
To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Mathematics (Calculus or Linear Algebra), and E - Eng...原创 2018-07-27 22:11:30 · 172 阅读 · 0 评论 -
PAT甲级A1025 PAT Ranking (25)
Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists w...原创 2018-08-14 22:15:56 · 127 阅读 · 0 评论 -
PAT甲级A1022 Digital Library (30)
A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number a...原创 2018-08-11 21:25:21 · 154 阅读 · 0 评论 -
PAT甲级A1016 Phone Bills (25)
A long-distance telephone company charges its customers by the following rules: Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When...原创 2018-08-02 20:20:42 · 413 阅读 · 0 评论 -
PAT甲级A1029 Median(25 分)
Given an increasing sequence S of N integers, the median is the number at the middle position. For example, the median of S1 = { 11, 12, 13, 14 } is 12, and the median of S2 = { 9, 10, 15, 16, 17 } is...原创 2018-08-17 22:06:22 · 662 阅读 · 0 评论 -
PAT甲级A1028 List Sorting (25)
Excel can sort records according to any column. Now you are supposed to imitate this function. Input Each input file contains one test case. For each case, the first line contains two integers N (&l...原创 2018-08-16 21:19:47 · 179 阅读 · 0 评论 -
PAT甲级A1037 Magic Coupon(25 分)
The magic shop in Mars is offering some magic coupons. Each coupon has an integer N printed on it, meaning that when you use this coupon with a product, you may get N times the value of that product b...原创 2018-08-27 21:17:40 · 178 阅读 · 0 评论 -
PAT甲级A1039 Course List for Student(25 分)
Zhejiang University has 40000 students and provides 2500 courses. Now given the student name lists of all the courses, you are supposed to output the registered course list for each student who comes ...原创 2018-08-29 22:04:13 · 188 阅读 · 0 评论 -
PAT甲级A1047 Student List for Course (25 分)
Zhejiang University has 40,000 students and provides 2,500 courses. Now given the registered course list of each student, you are supposed to output the student name lists of all the courses. Input S...原创 2019-01-17 21:01:02 · 236 阅读 · 0 评论 -
PAT甲级A1062 Talent and Virtue (25 分)
About 900 years ago, a Chinese philosopher Sima Guang wrote a history book in which he talked about people's talent and virtue. According to his theory, a man being outstanding in both talent and virt...原创 2019-01-24 17:39:32 · 231 阅读 · 0 评论 -
PAT甲级A1052 Linked List Sorting (25 分)
A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now give...原创 2019-01-19 20:14:21 · 138 阅读 · 1 评论 -
PAT甲级A1055 The World's Richest (25 分)
Forbes magazine publishes every year its list of billionaires based on the annual ranking of the world's wealthiest people. Now you are supposed to simulate this job, but concentrate only on the peopl...原创 2019-01-20 20:17:06 · 232 阅读 · 0 评论 -
PAT乙级B1035 插入与归并 (25 分)
根据维基百科的定义: 插入排序是迭代算法,逐一获得输入数据,逐步产生有序的输出序列。每步迭代中,算法从输入序列中取出一元素,将之插入有序序列中正确的位置。如此迭代直到全部元素有序。 归并排序进行如下迭代操作:首先将原始序列看成 N 个只包含 1 个元素的有序子序列,然后每次迭代归并两个相邻的有序子序列,直到最后只剩下 1 个有序的序列。 现给定原始序列和由某排序算法产生的中间序列,请你判断该...原创 2019-01-29 17:14:16 · 264 阅读 · 0 评论 -
PAT乙级B1015 德才论 (25)(25 分)
宋代史学家司马光在《资治通鉴》中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人。凡取人之术,苟不得圣人,君子而与之,与其得小人,不若得愚人。” 现给出一批考生的德才分数,请根据司马光的理论给出录取排名。 输入格式: 输入第1行给出3个正整数,分别为:N(<=10^5^),即考生总数;L(>=60),为录取最低分数线,即德分和才分均...原创 2018-05-21 22:32:43 · 306 阅读 · 0 评论