做题记录
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int peakNum(int* hight,int datasize) {
int res = 0;
int i = 0;
while (1) {
if (i >= datasize)//循环i次返回res
{
return res;
}
else
{
if (i == 0)
{
if (hight[i + 1] < hight[i])//最左边的情况
res += 1;
}
else if (i == datasize - 1)//最右边的情况
{
if (hight[i - 1] < hight[i])
{
res += 1;
}
}
else
{
if (hight[i - 1] < hight[i] && hight[i + 1] < hight[i])//中间的情况
{
res += 1;
}
}
}
i += 1;
}
}
int main()
{
//处理输入
char data[20000];
char* sep = ",";
fgets(data, 20000, stdin);//输入数据
char* token = strtok(data, sep);//用strtok函数拆分数据
int hight[1000];
int count = 0;
while (token != NULL)
{
hight[count] = atoi(token);//数据不为空就存入山峰高度数组
count++;
token = strtok(NULL,sep);//strtok会改变 data的值
//改变一次后 后面就不需要传入data了 直接使用就行
}
printf("%d",peakNum(hight,count));
}