考核题 7
#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { //1.以下是身份证号,输出身份证人的性别,年龄 NSLog(@"第一题"); NSString *idCard=@"210423198809040427"; //身份证倒数#2位是偶数则为女生 NSRange range1; NSRange range2; range1.length=1; range1.location=16; range2.length=4; range2.location=6; NSString *newid1=[idCard substringWithRange:range1]; NSString *newid2=[idCard substringWithRange:range2]; int num1=[newid1 intValue]; int result=num1/2; int num2=[newid2 intValue]; int age=2016-num2; if (result) { NSLog(@"性别:女 年龄:%d",age); }else{ NSLog(@"性别:男 年龄:%d",age); } //2.将字典的key从Z->A排序,按排序后的key的顺序,输出value,将value按字符串输出 NSLog(@"第二题"); NSDictionary *dict=@{@"R":@"e", @"T":@"e", @"D":@"b", @"S":@"u", @"K":@"a", @"A":@"s", @"O":@" ", @"N":@"p", @"B":@"b", @"J":@"u", @"F":@" ", @"U":@"t", @"H":@"l", @"E":@"j", @"Z":@"s", }; NSArray *dic=[dict allKeys]; NSArray *newdic=[dic sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) { return [obj2 compare:obj1]; }]; NSMutableString *newValue=[NSMutableString string]; for (int i;i<newdic.count;i++) { id key=[newdic objectAtIndex:i]; NSString *value=[dict objectForKey:key]; [newValue appendString:value]; } NSLog(@"%@",newValue); } return 0; }