C语言:在二维数组中,每行每列都按照递增的顺序排序,判断数组中是否包含一个数。

来源:互联网 时间:1970-01-01

#define _CRT_SECURE_NO_WARNINGS 1#include<stdio.h>#include<stdlib.h>int search_target(int arr[], int target, int ROW, int COLUMN){ int row = 0; int column = COLUMN - 1; if (arr != NULL && ROW > 0 && COLUMN > 0) { while (row < ROW && column >= 0) { if (arr[row * COLUMN + column] == target) { return 1; } else if (arr[row * COLUMN + column] < target) { ++row; } else if (arr[row * COLUMN + column] > target) { --column; } } return 0; } return 0;}int main(){ int arr[][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int ret = search_target(arr, 7, 3, 3); if (ret == 1) { printf("exist/n"); } else { printf("not exist/n"); } system("pause"); return 0;}

 

相关阅读:
Top