- 论坛徽章:
- 0
|
本帖最后由 hbmhalley 于 2011-12-14 11:35 编辑
回复 117# zhedahht
- //#define DATA
- #include <stdio.h>
- #include <stdlib.h>
- #include <assert.h>
- int main () {
- int i , j;
- #define mat(x,y) _mat[(x)*col+(y)]
- #ifdef DATA
- int row = 4 , col = 4;
- int _mat[] = {
- 1, 2, 8, 9,
- 2, 4, 9, 12,
- 4, 7, 10, 13,
- 6, 8, 11, 15
- };
- #else
- printf ("Input the row and col\n");
- int row , col;
- assert (scanf ("%d%d" , &row , &col) == 2);
- assert (row>0 && col>0);
- int *_mat = (int*)calloc(row*col , sizeof(int));
- printf ("Input the %d*%d matrix\n" , row , col);
- for (i=0 ; i<row ; ++i)
- for (j=0 ; j<col ; ++j)
- assert (scanf ("%d" , &mat(i,j)) == 1);
- #endif
- for (i = 0 ; i<row ; ++i)
- for (j = 0 ; j<col ; ++j) {
- assert (i==0 || mat(i,j) >= mat(i-1,j));
- assert (j==0 || mat(i,j) >= mat(i,j-1));
- }
- int query;
- while ( printf ("Input the number to find, or 'q' to quit\n"), scanf ("%d" , &query) == 1 ) {
- i = row-1; j = 0;
- while ( i>=0 && j<col && mat(i,j)!=query )
- mat(i,j)<query ? ++j : --i;
- if ( i>=0 && j<col )
- printf ("Existent : (%d,%d)\n" , i , j);
- else
- printf ("Inexistent\n");
- }
- }
复制代码 22L的。代码很重要么 |
|