题解 | #有序序列判断# #函数
有序序列判断
https://www.nowcoder.com/practice/22e87f8a8d764a6582710f38d1b40c6e
#include <stdio.h> #include <stdlib.h> #include <string.h> char* judge(int *a,int n); int main() { int a, n,i; scanf("%d",&n); int *arr = (int*)malloc(n*sizeof(int)); for(i=0;i<n;i++) scanf("%d",arr+i); puts(judge(arr, n)); return 0; } char* judge(int *a,int n) { int i,sum = 0; char *a1 = (char*)malloc(sizeof(char)*20); char *a2 = (char*)malloc(sizeof(char)*20); strcpy(a1,"unsorted"); strcpy(a2,"sorted"); for(i = 0;i<n-1;i++) { if(*(a+i)>*(a+i+1)) sum++; } if(sum==0||sum==(n-1)) return a2; else return a1; }