c语言编程,判断一个长度为n的整型数组是否有序。升序返回1,降序返回-1,无序返回0,若所有元素

相等则返回2

//刚写的code,测试通过
#include<stdio.h>
#define N 4
int check_order(int *tar, int n){
int i;
i = 1;
if(tar[0] == tar[1]){
while(i<n&&tar[i] == tar[i-1])
i++;
if(i == n)
return 2;
}
i=1;
if(tar[0] > tar[1]){
while(i<n&&tar[i-1] >tar[i])
i++;
if(i == n)
return -1;
}
i=1;
if(tar[0] < tar[1]){
while(i<n&&tar[i-1] <tar[i])
i++;
if(i == n)
return 1;
}
return 0;
}
int main(){
int a[N];
int i;
int flag;
for(i = 0; i<N;i ++)
scanf("%d", &a[i]);
printf("%d\n", check_order(a, N));
return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-01-27

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网