模拟考试第一道程序员,求大佬解答
import java.util.Scanner;public class Main { public static void main(String[] args) { Scanner sc = new
Scanner(System.in);
int n = sc.nextInt();
if(n<1||n>100000){ System.out.println("输入错误");
return ;}
long A[]=new long[n];
for(int i = 0; i < n; i ){
A[i] = sc.nextLong();
if( A[i]<1|| A[i]>1000000000)
{
System.out.println("输入错误");
return ; } }
int count=1;
for(int i=1;i<n-1;i )
{
if((A[i]>A[i-1]&&A[i]>A[i 1])||(A[i]<A[i-1]&&A[i]<A[i 1]))
{ count ; i ; }
}
System.out.println(count);
}}
int n = sc.nextInt();
if(n<1||n>100000){ System.out.println("输入错误");
return ;}
long A[]=new long[n];
for(int i = 0; i < n; i ){
A[i] = sc.nextLong();
if( A[i]<1|| A[i]>1000000000)
{
System.out.println("输入错误");
return ; } }
int count=1;
for(int i=1;i<n-1;i )
{
if((A[i]>A[i-1]&&A[i]>A[i 1])||(A[i]<A[i-1]&&A[i]<A[i 1]))
{ count ; i ; }
}
System.out.println(count);
}}