ObjC语法练习 冒泡排序、选择排序、矩阵相乘

用OC实现的冒泡排序、选择排序、矩阵相乘,纯粹是用来练习语法。

冒泡排序,程序如下:

void bubbleSort()
{
//初始化数组
NSMutableArray *array1 = [[NSMutableArray alloc] initWithCapacity:];
[array1 addObject:@""];
[array1 addObject:@""];
[array1 addObject:@""];
[array1 addObject:@""];
[array1 addObject:@""];
[array1 addObject:@""];
[array1 addObject:@""];
[array1 addObject:@""]; NSInteger count = array1.count; for(int i = ; i < count; ++i){
for(int j = ; j < count - i - ; ++j){
NSInteger before = [[array1 objectAtIndex:j] integerValue];
NSInteger after = [[array1 objectAtIndex:j + ] integerValue];
if(before > after){
NSInteger temp = before;
[array1 setObject:[NSString stringWithFormat:@"%ld", after] atIndexedSubscript:j];
[array1 setObject:[NSString stringWithFormat:@"%ld",temp ] atIndexedSubscript:j + ];
}
}
} for(int i = ; i < count; ++i){
NSLog(@"%@",[array1 objectAtIndex:i]);
}
}

选择排序,程序如下:

void selectSort()
{
//初始化数组
NSMutableArray *array1 = [[NSMutableArray alloc] initWithCapacity:];
[array1 addObject:@""];
[array1 addObject:@""];
[array1 addObject:@""];
[array1 addObject:@""];
[array1 addObject:@""];
[array1 addObject:@""];
[array1 addObject:@""];
[array1 addObject:@""]; NSInteger count = array1.count; for(int i = ; i < count - ; ++i){
NSInteger min = i;
for(int j = i + ; j < count; ++j){
NSInteger minNum = [[array1 objectAtIndex:min] integerValue];
NSInteger num = [[array1 objectAtIndex:j] integerValue];
if(minNum > num){
min = j;
}
}
NSInteger changeNum = [[array1 objectAtIndex:min] integerValue];
NSInteger temp = [[array1 objectAtIndex:i] integerValue];
[array1 setObject:[NSString stringWithFormat:@"%ld",changeNum] atIndexedSubscript:i];
[array1 setObject:[NSString stringWithFormat:@"%ld",temp] atIndexedSubscript:min];
} for(int i = ; i < count; ++i){
NSLog(@"%@",[array1 objectAtIndex:i]);
}
}

矩阵相乘,程序如下:

int main(int argc, const char * argv[]) {
@autoreleasepool { //bubbleSort();
//selectSort(); //矩阵相乘
//初始化矩阵1,矩阵1为:
// 1 0 2
// -1 3 1
NSMutableArray *array1 = [[NSMutableArray alloc] initWithCapacity:];
NSMutableArray *row1 = [[NSMutableArray alloc] initWithCapacity:];
NSMutableArray *row2 = [[NSMutableArray alloc] initWithCapacity:];
[row1 addObject:@""];
[row1 addObject:@""];
[row1 addObject:@""];
[row2 addObject:@"-1"];
[row2 addObject:@""];
[row2 addObject:@""];
[array1 addObject:row1];
[array1 addObject:row2];
//初始化矩阵2,矩阵2为
// 3 1
// 2 1
// 1 0
NSMutableArray *array2 = [[NSMutableArray alloc] initWithCapacity:];
NSMutableArray *newrow1 = [[NSMutableArray alloc] initWithCapacity:];
NSMutableArray *newrow2 = [[NSMutableArray alloc] initWithCapacity:];
NSMutableArray *newrow3 = [[NSMutableArray alloc] initWithCapacity:];
[newrow1 addObject:@""];
[newrow1 addObject:@""];
[newrow2 addObject:@""];
[newrow2 addObject:@""];
[newrow3 addObject:@""];
[newrow3 addObject:@""];
[array2 addObject:newrow1];
[array2 addObject:newrow2];
[array2 addObject:newrow3]; //第一个矩阵的行数
NSInteger numRow1 = array1.count;
//第一个矩阵的列数
NSArray *Col = [array1 objectAtIndex:];
NSInteger numCol1 = Col.count;
NSLog(@"numRow1 = %ld",numRow1);
NSLog(@"numCol1 = %ld",numCol1); //第二个矩阵的列
NSArray *Col2 = [array2 objectAtIndex:];
NSInteger numCol2 = Col2.count;
NSLog(@"numCol2 = %ld",numCol2); NSMutableArray *array3 = [[NSMutableArray alloc] initWithCapacity:numRow1];
//开始矩阵相乘
for(int i = ; i < numRow1; ++i){
NSMutableArray *temprow = [[NSMutableArray alloc] initWithCapacity:numCol2];
for(int j = ; j < numCol2; ++j){
NSInteger sum = ;
for(int k = ; k < numCol1; ++k){
NSInteger num1 = [[[array1 objectAtIndex:i] objectAtIndex:k] intValue];
NSInteger num2 = [[[array2 objectAtIndex:k] objectAtIndex:j] intValue];
sum = sum + (num1 * num2);
}
NSString *tempString = [NSString stringWithFormat:@"%ld",sum ];
[temprow addObject:tempString];
}
[array3 addObject:temprow];
}
for(int i = ; i < numRow1;++i){
for(int j = ; j < numCol2; ++j){
NSLog(@"%@",[[array3 objectAtIndex:i] objectAtIndex:j]);
}
}
}
return ;
}
上一篇:Go语言及Web框架Beego环境


下一篇:Leetcode#70. Climbing Stairs(爬楼梯)