Created
April 25, 2017 20:07
-
-
Save thinkphp/c402b7c7d36c2278fb1fe76e079c3e5d to your computer and use it in GitHub Desktop.
Given a matrix of m*n size, the task is to count all the rows in a matrix that are sorted either in strictly increasing order or in strictly decreasing order?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
int checkSort(int v[101],int c); | |
int main() | |
{ | |
int T, | |
i,j,mat[101][101],rows,cols; | |
scanf("%d", &T); | |
while(T--) { | |
//read the num of rows and cols | |
scanf("%d %d", &rows, &cols); | |
//read the matrix | |
for(i = 0; i < rows; i++) { | |
for(j = 0; j < cols; j++) { | |
scanf("%d", &mat[i][j]); | |
} | |
} | |
int count = 0; | |
for(i = 0; i < rows; i++) { | |
int r = checkSort(mat[i],cols); | |
if(r == 1) count++; | |
} | |
printf("%d\n", count); | |
} | |
return 0; | |
}; | |
int checkSort(int vec[], int cols) { | |
int i, a = 0, b = 0; | |
for(i = 0; i < cols - 1; i++ ){ | |
if(vec[i] < vec[i + 1]) a++; | |
if(vec[i] > vec[i + 1]) b++; | |
} | |
if(a+1 == cols || b+1 == cols) return 1; | |
else | |
return 0; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment