CCF CSP Brush Question Record 12-201604-1 Breakpoint Count (Java)

Question number: 201604-1
Question name: Breakpoint count
time limit: 1.0s
Memory limit: 256.0MB
Problem Description:

Problem Description

  Given n integers represent the sales volume of a store for n consecutive days. If the sales volume increased before a certain day and the sales volume decreased the following day, then this day is called the breakpoint. Conversely, if the sales volume decreased before and the sales volume increased the following day, the day was also called the breakpoint. The other days are not turning points. As shown in the figure below, day 3 and day 6 are turning points.


  Given n integers a 1,  a 2, …,  an representing sales volume, please calculate how many turning points there are in these days.
  In order to reduce ambiguity, we have given data guarantee: the sales volume of two adjacent days in these n days is always different, that is, ai -1≠ ai . Note that if the two days are not adjacent, the sales volume may be the same.

Input format

  The first line of input contains an integer n .
  The second line contains n integers, separated by spaces, representing a 1,  a 2, …,  an respectively .

Output format

  Output an integer, indicating the number of breakpoints.

Sample input

7
5 4 1 2 3 6 4

Sample output

2

Evaluation use case scale and conventions

  All evaluation use cases satisfy: 1 ​​≤  n  ≤ 1000, and the daily sales volume is a non-negative integer not exceeding 10,000.

 

import java.util.Scanner;
public class 折点计数 {
	public static void main(String args[]){
		Scanner sc =new Scanner(System.in);
		int n=sc.nextInt();
		int[] a=new int[n];
		for(int i=0;i<n;i++){
			a[i]=sc.nextInt();
		}
		int res=0;
		for(int 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])){
				res++;
			}
		}
		System.out.println(res);
	}
}

 

Guess you like

Origin blog.csdn.net/m0_37483148/article/details/108308250