首行缩进问题求解

给你一个数组(长度为n),数组第i位表示文档第i行要缩进多少格,问你要完成这个数组要求的操作最少要操作多少次(比如一个1,2,3的数组,可以选中三行同时缩进一格,然后选中后两行缩进一格,最后选中最后一行缩进一格,不允许选中不连续的行,缩进一格为一次操作)

作者:远古卷轴
链接:https://www.nowcoder.com/discuss/386476855904284672?sourceSSR=search
来源:牛客网

#include

/*
@return value
1:top
-1:bot
0:neither
*/
int top_or_bot(int a,int b,int c)
{
if(b>a && b>c)
    {
        return 1;
    }
    else if(b    {
        return -1;
    }
    else
    {
        return 0;
    }
}

int tab_min_nums(int *array, int len)
{
    int new_array[10000]={0};
    int new_len=0;
    int top_sum=0;
    //int top_nums=0;
    int bot_sum=0;
    //int bot_nums=0;
    int *p;
    int ret;
    int a,b,c;
    
    new_array[new_len]=*array;
    new_len++;
    for(int i=1;i    {
        if(new_array[new_len-1]!=*(array+i))
        {
            new_array[new_len]=*(array+i);
            new_len++;
        }
        
    }
    
    p=new_array;
    for(int i=0;i    {
        b=*(p+i);
        a=(0==i)?0:*(p+i-1);
        c=(len-1==i)?0:*(p+i+1);
        ret = top_or_bot(a,b,c);
        if(ret)
        {
            if(1==ret)
            {
                top_sum=top_sum+b;
            }
            else
            {
                bot_sum=bot_sum+b;
            }
        }
    }
    
    return top_sum-bot_sum;
    
}

int main()
{
    int array[]={9,9,9,2,2,1,1,1,4,3,2};
    
    printf("%d\n",tab_min_nums(array,sizeof(array)/sizeof(int)));
    
    printf("Hello, World\n");
    return(0);
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务