#include <iostream>
using namespace std;
int main() {
int n=0,count=0;
cin>>n;
//yset:昨天的值 tod:今天的值 yest_c:前天与昨天的差值 tid_c:今天与昨天的差值
int yest=0, tod=0, yest_c = 0, tod_c =0;
for(int i=0; i<n; i++){
cin>>tod;
if(i == 0)
yest=tod;//第一天
else{
tod_c = tod - yest; // 今天与昨天的差值
if((tod_c>0 && yest_c <0) || (tod_c<0 && yest_c>0)) //如果出现折点
count++;
yest_c = tod_c; // 今天的差值变成昨天的差值,进入下一天
}
}
cout<<count<<endl;
return 0;
}
·
求大佬们看看问题在哪里
最好还是用一个数组,因为你需要比较3个日期
/* CCF201604-1 折点计数 */
#include <stdio.h>
#define N 1000
int a[N];
int main(void)
{
int n, i, count;
// 读入数据
scanf("%d", &n);
for(i=0; i<n; i++)
scanf("%d", &a[i]);
// 计算折点
count = 0;
for(i=1; i<n-1; i++)
if((a[i-1] < a[i] && a[i] > a[i+1]) || (a[i-1] > a[i] && a[i] < a[i+1]))
count++;
// 输出结果
printf("%d\n", count);
return 0;
}