学生排序题

//  // 创建学生类 姓名 学号 年龄 成绩

// 创建五个学生对象,加入数组, 分别按姓名升序 学号降序 年龄升序 成绩降序排序

main.m

 1 #import <Foundation/Foundation.h>
 2 #import "Student.h"
 3 int main(int argc, const char * argv[]) {
 4     @autoreleasepool {
 5         // arc4random()%10  0---9
 6         //
 7         Student * s1 = [[Student alloc] initWithName:@"Jack" stuId:arc4random()%10+1 age:arc4random()%10+1 andScore:arc4random()%41 + 60];
 8         // 60 - 100
 9
10         Student * s2 = [[Student alloc] initWithName:@"Tom" stuId:arc4random()%10+1 age:arc4random()%10+1 andScore:arc4random()%41 + 60];
11
12         Student * s3 = [[Student alloc] initWithName:@"Lily" stuId:arc4random()%10+1 age:arc4random()%10+1 andScore:arc4random()%41 + 60];
13
14         Student * s4 = [[Student alloc] initWithName:@"Lucy" stuId:arc4random()%10+1 age:arc4random()%10+1 andScore:arc4random()%41 + 60];
15         Student * s5 = [[Student alloc] initWithName:@"Mark" stuId:arc4random()%10+1 age:arc4random()%10+1 andScore:arc4random()%41 + 60];
16
17 //        NSArray * arr = @[s1,s2,s3,s4,s5];
18         NSMutableArray * arr = [NSMutableArray array];
19         [arr addObject:s1];
20         [arr addObject:s2];
21         [arr addObject:s3];
22         [arr addObject:s4];
23         [arr addObject:s5];
24         // 成绩 降序
25         for (int i = 0; i< [arr count]; i++) {
26             for (int j = 0; j < [arr count] - i -1; j++) {
27                 Student * s1 = arr[j];
28                 Student * s2 = arr[j+1];
29                 if ([s1 isStuScoreLessThanAnother:s2]) {
30                     [arr exchangeObjectAtIndex:j withObjectAtIndex:j+1];
31                 }
32             }
33         }
34         NSLog(@"%@",arr);
35
36         NSArray * arr1 = [arr sortedArrayUsingSelector:@selector(isStuScoreLessThanAnother:)];
37         NSLog(@"%@",arr1);
38
39         // 年龄  左 < 右
40
41         NSArray * arr2 = [arr sortedArrayUsingComparator:^NSComparisonResult(Student * s1, Student * s2) {
42             return [s1 age] > [s2 age];
43         }];
44
45         NSLog(@"%@",arr2);
46
47
48         NSArray * arr3 = [arr sortedArrayUsingComparator:^NSComparisonResult(Student * obj1, Student * obj2) {
49             return [obj1 age] > [obj2 age];
50         }];
51         NSLog(@"%@",arr3);
52
53         NSArray * arr4 = [arr sortedArrayUsingComparator:^NSComparisonResult(Student * obj1, Student * obj2) {
54             return [obj1 score] < [obj2 score];
55         }];
56         NSLog(@"%@",arr4);
57
58
59     }
60     return 0;
61 }

main.m

 1 #import <Foundation/Foundation.h>
 2
 3 @interface Student : NSObject {
 4     NSString * _name;
 5     int _stuId;
 6     int _age;
 7     int _score;
 8 }
 9 - (id)initWithName:(NSString *)name stuId:(int)stuId age:(int)age andScore:(int)score;
10
11 - (int)score;
12 - (int)age;
13 - (BOOL)isStuScoreLessThanAnother:(Student *)s2;
14 @end

Student.h

 1 #import "Student.h"
 2
 3 @implementation Student
 4 - (id)initWithName:(NSString *)name stuId:(int)stuId age:(int)age andScore:(int)score {
 5     if (self = [super init]) {
 6         _name = name;
 7         _age = age;
 8         _stuId = stuId;
 9         _score = score;
10     }
11     return self;
12 }
13
14 - (int)score {
15     return _score;
16 }
17
18 - (NSString *)description {
19     return [NSString stringWithFormat:@"name:%@ stuId:%d age:%d score:%d",_name,_stuId,_age,_score];
20 }
21
22 - (int)age {
23     return _age;
24 }
25
26 - (BOOL)isStuScoreLessThanAnother:(Student *)s2 {
27     return [self score] < [s2 score];
28 }
29
30
31 @end

Student.m

时间: 2024-12-28 22:01:07

学生排序题的相关文章

【算法学习记录-排序题】【PAT A1012】The Best Rank

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 Algrbra), and E - English. At the mean time, we encourage students by e

排序题如何进行数据分析

今天学习遇见排序题, 请教刘阳好基友,给讲讲 1. 首先看数据, 是什么样的 2. 怎么回事 填空的位置, 例如P1Q6A代表第一个空, 以此类推... 那P1Q6A下面的1代表A, P1Q6B下面的3代表c,第一行的数据则代表A, C, B, E, D 分配权重, 5, 4, 3, 2, 1,--> 看第二个图, 权重    5 4 3 2  1 空1   空2 空3 空4 空5 1(权重5)  3 2 5 4 3(权重5) 4 5 1 2 ==========================

【算法学习记录-排序题】【PAT A1025】PAT Ranking

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 will be merged immediately after the test. Now it i

【算法学习记录-排序题】【PAT A1062】Talent and Virtue

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 virtue must be a "sage(圣人)"; being less excellent

每日一练:排序题:一大一小,依次类推题

/*描述:有一组数(43,14,6,9,3,27,38,19,5等) 排序:将最大的放在第一位,最小放在第二位,剩下的最大的放在第三位,最小的放在第四位,以此类推 输出排序结果*/ package everyDay; import java.util.Arrays; public class SortTest { /** * 实现思路:1.数组的排序小大 * 2.判断数组奇偶 * 2.1偶数:除2,小前半部分b数组,大后半部分c数组长度 * 2.2 再分别把a的值给b和c,又两个循环 * 2.3

Luogu 1583 - 魔法照片 - [简单排序题]

题目链接:https://www.luogu.org/problemnew/show/P1583 题目描述一共有n(n≤20000)个人(以1--n编号)向佳佳要照片,而佳佳只能把照片给其中的k个人.佳佳按照与他们的关系好坏的程度给每个人赋予了一个初始权值W[i].然后将初始权值从大到小进行排序,每人就有了一个序号D[i](取值同样是1--n).按照这个序号对10取模的值将这些人分为10类.也就是说定义每个人的类别序号C[i]的值为(D[i]-1) mod 10 +1,显然类别序号的取值为1--

POJ 2367 非常基本的拓扑排序题 用GCC可以AC

问题描述 The system of Martians' blood relations is confusing enough. Actually, Martians bud when they want and where they want. They gather together in different groups, so that a Martian can have one parent as well as ten. Nobody will be surprised by a

PAT A1113 Integer Set Partition (25 分)——排序题

Given a set of N (>1) positive integers, you are supposed to partition them into two disjoint sets A?1?? and A?2?? of n?1?? and n?2?? numbers, respectively. Let S?1?? and S?2?? denote the sums of all the numbers in A?1?? and A?2??, respectively. You

考研机试真题(一)之排序

转载请标明出处:牟尼的专栏 http://blog.csdn.net/u012027907 题目1202:排序 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:10071 解决:3549 题目描述: 对输入的n个数进行排序并输出. 输入: 输入的第一行包括一个整数n(1<=n<=100). 接下来的一行包括n个整数. 输出: 可能有多组测试数据,对于每组数据,将排序后的n个整数输出,每个数后面都有一个空格. 每组测试数据的结果占一行. 样例输入: 4 1 4 3 2 样例输出: 1