牛客672914934号 level
获赞
0
粉丝
0
关注
1
看过 TA
0
长春理工大学
2018
嵌入式软件开发
IP属地:上海
暂未填写个人简介
私信
关注
给你一个数组(长度为n),数组第i位表示文档第i行要缩进多少格,问你要完成这个数组要求的操作最少要操作多少次(比如一个1,2,3的数组,可以选中三行同时缩进一格,然后选中后两行缩进一格,最后选中最后一行缩进一格,不允许选中不连续的行,缩进一格为一次操作)作者:远古卷轴链接:https://www.nowcoder.com/discuss/386476855904284672?sourceSSR=search来源:牛客网#include <stdlib.h>/*@return value1:top-1:bot0:neither*/int top_or_bot(int a,int b,int c){if(b>a && b>c){return 1;}else if(b<a && b<c){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<len;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<new_len;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);}
0 点赞 评论 收藏
分享

创作者周榜

更多
关注他的用户也关注了:
牛客网
牛客网在线编程
牛客网题解
牛客企业服务