日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

学生排序题

發(fā)布時間:2025/4/14 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 学生排序题 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

//? // 創(chuàng)建學(xué)生類 姓名 學(xué)號 年齡 成績

?

// 創(chuàng)建五個學(xué)生對象,加入數(shù)組, 分別按姓名升序 學(xué)號降序 年齡升序 成績降序排序

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

?

轉(zhuǎn)載于:https://www.cnblogs.com/gwkiOS/p/4931988.html

總結(jié)

以上是生活随笔為你收集整理的学生排序题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。