题解 | #有序序列判断#

有序序列判断

http://www.nowcoder.com/practice/22e87f8a8d764a6582710f38d1b40c6e

#include<stdio.h>
#include<stdbool.h>
int main(void)
{
    int n;
    scanf("%d",&n);
    int *a=(int *)malloc(sizeof(int)*n);
//    bool issorted=true;//定义一个bool变量用来判断数组是sorted还是unsorted;
    int sortup=0,sortdown=0;//一开始看错了题目,以为要判断是否升序序列,用了以上注释方式,提交错误后,用了记录个数的方式
    for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
    for(int i=1;i<n;i++)
    {
        if(a[i]<a[i-1])
        {
//            issorted=false;
            sortdown++;
        }
        else
            sortup++;
    }
    if(sortup==0||sortdown==0)//如果全部为升序或者全部为降序,则为sorted,否则为nusorted
        printf("sorted\n");
    else
        printf("unsorted");
    return 0;
}
全部评论

相关推荐

哥_留个offer先:跟他说,你这个最好用c#,微软就用c#Java不适合这个项目
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务