算法3

作者: 7_c5dc | 来源:发表于2019-05-18 23:44 被阅读0次
    image.png image.png
    
    bool find(int* matrix, int rows, int columns, int number) {
        bool found = false;
        int row = 0;
        int column = columns - 1;
    
        if (matrix != nil && rows > 0 && columns > 0 ) {
    
            while (row < rows && column < columns) {
                int temp = matrix[row * columns + column];
                if (temp == number) {
                    found = true;
                    break;
                } else if (temp < number) {
                    row ++;
                } else {
                    column --;
                }
            }
        }
        return found;
    }
    
    int main(int argc, const char * argv[]) {
    
        int a[16] = {1,2,8,9, 2,4,9,12, 4,7,10,13, 6,8,11,15};
        NSLog(@"%d", find(a, 4, 4, 7));
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:算法3

          本文链接:https://www.haomeiwen.com/subject/pagxzqtx.html