(c#)二维数组的查找

 class MyArray
    {
        static void Main(string[] args)
        {
            
            int[,] a = new int[3, 3] {
                { 1,2,3},
                { 2,4,5},
                { 5,6,7}
                };

           bool isfind= find(3, a);
            Console.Write(isfind);


        }
        public static bool find(int target, int[,] arry)
        {
            int row = arry.GetLength(0);
            int col = arry.GetLength(1);
            int i, j;
            for ( i = row-1,j=0; i >=0&&j<col ; )
            {
                if (target == arry[i, j])
                    return true;
                if (target < arry[i, j])
                {
                    i--;
                    continue;
                }
                if (target > arry[i, j])
                {
                    j++;
                    continue;
                }
            }
            return false;
        }
    }

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页