zoukankan      html  css  js  c++  java
  • ObjC语法练习 冒泡排序、选择排序、矩阵相乘

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

    冒泡排序,程序如下:

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

    选择排序,程序如下:

    void selectSort()
    {
        //初始化数组
        NSMutableArray *array1 = [[NSMutableArray alloc] initWithCapacity:8];
        [array1 addObject:@"5"];
        [array1 addObject:@"10"];
        [array1 addObject:@"8"];
        [array1 addObject:@"12"];
        [array1 addObject:@"9"];
        [array1 addObject:@"4"];
        [array1 addObject:@"20"];
        [array1 addObject:@"14"];
        
        NSInteger count = array1.count;
        
        for(int i = 0; i < count - 1; ++i){
            NSInteger min = i;
            for(int j = i + 1; 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 = 0; 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:2];
            NSMutableArray *row1 = [[NSMutableArray alloc] initWithCapacity:3];
            NSMutableArray *row2 = [[NSMutableArray alloc] initWithCapacity:3];
            [row1 addObject:@"1"];
            [row1 addObject:@"0"];
            [row1 addObject:@"2"];
            [row2 addObject:@"-1"];
            [row2 addObject:@"3"];
            [row2 addObject:@"1"];
            [array1 addObject:row1];
            [array1 addObject:row2];
            //初始化矩阵2,矩阵2为
            //   3    1
            //   2    1
            //   1    0
            NSMutableArray *array2 = [[NSMutableArray alloc] initWithCapacity:3];
            NSMutableArray *newrow1 = [[NSMutableArray alloc] initWithCapacity:2];
            NSMutableArray *newrow2 = [[NSMutableArray alloc] initWithCapacity:2];
            NSMutableArray *newrow3 = [[NSMutableArray alloc] initWithCapacity:2];
            [newrow1 addObject:@"3"];
            [newrow1 addObject:@"1"];
            [newrow2 addObject:@"2"];
            [newrow2 addObject:@"1"];
            [newrow3 addObject:@"1"];
            [newrow3 addObject:@"0"];
            [array2 addObject:newrow1];
            [array2 addObject:newrow2];
            [array2 addObject:newrow3];
            
            //第一个矩阵的行数
            NSInteger numRow1 = array1.count;
            //第一个矩阵的列数
            NSArray  *Col = [array1 objectAtIndex:0];
            NSInteger numCol1 = Col.count;
            NSLog(@"numRow1 = %ld",numRow1);
            NSLog(@"numCol1 = %ld",numCol1);
            
            //第二个矩阵的列
            NSArray *Col2 = [array2 objectAtIndex:0];
            NSInteger numCol2 = Col2.count;
            NSLog(@"numCol2 = %ld",numCol2);
            
            NSMutableArray *array3 = [[NSMutableArray alloc] initWithCapacity:numRow1];
            //开始矩阵相乘
            for(int i = 0; i < numRow1; ++i){
                NSMutableArray *temprow = [[NSMutableArray alloc] initWithCapacity:numCol2];
                for(int j = 0; j < numCol2; ++j){
                    NSInteger sum = 0;
                    for(int k = 0; 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 = 0; i < numRow1;++i){
                for(int j = 0; j < numCol2; ++j){
                    NSLog(@"%@",[[array3 objectAtIndex:i] objectAtIndex:j]);
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    社会科学 | 游戏开发 | 浅谈游戏中的符号象征主义
    数据挖掘 | 数据隐私(4) | 差分隐私 | 差分隐私概论(下)(Intro to Differential Privacy 2)
    数据挖掘 | 数据隐私(3) | 差分隐私 | 差分隐私概论(上)(Intro to Differential Privacy 1)
    数据挖掘 | 数据隐私(2) | 差分隐私 | 数据重构化攻击(Reconstruction Attacks)
    数据挖掘 | 数据隐私(1) | 差分隐私 | 挑战数据隐私(Some Attempts at Data Privacy)
    Opencv | 图形学 | Mingw64 | 如何正确地用MinGW64编译与配置vscode的Opencv环境
    机器学习 | 强化学习(8) | 探索与开发(Exploration and Exploitation)
    机器学习 | 强化学习(7) | 融合学习与规划(Integrating Learning and Planning)
    机器学习 | 强化学习(5) | 价值函数拟合(Value Function Approximation)
    C#进阶
  • 原文地址:https://www.cnblogs.com/acBool/p/5164659.html
Copyright © 2011-2022 走看看