|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
求解
- public class Test01 {
- public static void main(String[] args) {
- Scanner scanner=new Scanner(System.in);
- //输入n
- int n = scanner.nextInt();
- //输入n个点的高度
- int[] nums=new int[n];
- for (int i=0;i<n;i++){
- nums[i]=scanner.nextInt();
- }
- //输出山的个数
- System.out.println(countMountain(n,nums));
- }
- //计算山的个数
- public static Integer countMountain(int n,int[] nums){
- //如果点的个数小于3则不能构成山,所以直接返回0
- if (nums.length<3){
- return 0;
- }
- int res=0;
- //从索引值为1的点开始遍历,分别向两端搜索小于当前索引值的点的个数,两个值的乘积即为当前索引值为山峰的山的个数
- for (int i=1;i<nums.length-1;i++){
- int leftNums=0;
- for (int j=0;j<i;j++){
- if (nums[j]<nums[i]){
- leftNums++;
- }
- }
- int rightNums=0;
- for (int j=i+1;j<nums.length;j++){
- if (nums[j]<nums[i]){
- rightNums++;
- }
- }
- res+=leftNums*rightNums;
- }
- return res;
- }
- }
复制代码
|
|