zoukankan      html  css  js  c++  java
  • c语言杨氏矩阵算法

    杨氏矩阵
    有一个二维数组.
    数组的每行从左到右是递增的,每列从上到下是递增的.
    在这样的数组中查找一个数字是否存在。
    时间复杂度小于O(N);
    数组:
    1 2 3
    2 3 4
    3 4 5

    1 3 4
    2 4 5
    4 5 6

    1 2 3

    4 5 6

    7 8 9

    #define _CRT_SECURE_NO_WARNINGS
    #include<stdio.h>
    #include<stdlib.h>
    int find(int arr[3][3], int rows, int cols, int data)
    {
        int i = 0;
        int j = cols - 1;
        while ((rows>i) && (j>0))
        {
            if (arr[i][j] > data)
            {
                j--;
            }
            else if (arr[i][j] < data)
            {
                i++;
            }
            else
            {
                return 1;
            }
        }
        return 0;
    }
    int main()
    {
        int arr[3][3] = { { 1, 4, 7 }, { 4, 5, 8 }, { 7, 11, 12 } };
        int data = 0;
        printf("please input a number: ");
        scanf("%d", &data);
    
        int be_exist = find(arr, 3, 3, data);
    
        if (be_exist)
        {
            printf("%d is exist
    ", data);
        }
        else
        {
            printf("%d is not exist
    ", data);
        }
        system("pause");
        return 0;
    }
  • 相关阅读:
    java-day21
    java-day20
    java-day19
    java-day18
    java-day17
    java-day16
    java-day15
    java-day14
    python-day06
    java-day13
  • 原文地址:https://www.cnblogs.com/Duikerdd/p/9905923.html
Copyright © 2011-2022 走看看