百度

1、度度熊想去商场买一顶帽子,商场里有N顶帽子,有些帽子的价格可能相同。度度熊想
买一顶价格第三便宜的帽子,问第三便宜的帽子价格是多少? 
输入描述: 
首先输入一个正整数N(N <= 50),接下来输入N个数表示每顶帽子的价格(价格均是
正整数,且小于等于1000) 
输出描述: 
如果存在第三便宜的帽子,请输出这个价格是多少,否则输出-1 
输入例子1: 
10 
10 10 10 10 20 20 30 30 40 40 
输出例子1: 
30 

 

package com.baidu;

import java.util.Arrays;
import java.util.Scanner;

/**
 * Hat
 *
 * @author : lao
 * @date : 2019/10/18  23:21
 */
public class Hat {


    public static void main(String[] args) {
        int n;
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        int[] price = new int[n];
        for (int i = 0; i < n; i++) {
            price[i] = scanner.nextInt();
        }
        Arrays.sort(price);
        int flag = 1;
        for(int i = 0; i < n - 1; i++){
            if(price[i] < price[i + 1]){
                flag++;
            }
            //找出第三便宜的输出
            if(flag == 3){
                System.out.println(price[i+1]);
                break;
            }
        }
        if(flag != 3){
            System.out.println(-1);
        }

    }
}

 2、一个数轴上共有 N 个点,第一个点的坐标是度度熊现在位置,第 N-1 个点是度度熊的家。现在他需要依次的从0号坐标走到N-1号坐标。 
但是除了0号坐标和N-1号坐标,他可以在其余的N-2个坐标中选出一个点,并直接将这个点忽略掉,问度度熊回家至少走多少距离? 
解答: 
 从N-2 个坐标中选出一个点,并直接将这个点忽略掉。直接忽略一个点只会直接影响到,这个节点前后节点的距离。这个 影响的距离我们暂且命名为优化距离,将所有节点按顺序组成三个节点的集合,通过这种方式只需要通过一次循环便能得到结果。 
 
 
优化距离越大说明如果去掉这个集合的中点元素将会使得总距离越短,下面上代码。

package com.baidu;

import java.util.Scanner;

/**
 * Home
 *
 * @author : lao
 * @date : 2019/10/19  16:52
 */
public class Home {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int length = scanner.nextInt();
        int[] array = new int[length];
        for (int i = 0; i < length; i++) {
            array[i] = scanner.nextInt();
        }
        /**
         * sum 总距离
         * repetition  三个节点 中被重复计算的总距离
         * select 优化距离最大的  三个节点两两相加的距离
         * add 三个结尾距离为max 中 头尾节点的距离
         * last最后三个节点中 尾距离没有被计算两次 需要加上
         * optimizeDistance 优化距离
         */

        int sum=0,repetition=0,select=0,add=0,
                last=0,optimizeDistance = 0;
        for(int i = 0;i <= length - 3; i++){
            int begin = array[i];
            int mid = array[i + 1];
            int end = array[i + 2];
            //三个点之间的距离
            int threePointDistance = Math.abs(mid - begin) + Math.abs(end - mid);
            //两个点之间的距离  即被多次计算待会需要减掉的距离
            int twoPointDistance = Math.abs(end - mid);
            int contrast = threePointDistance - Math.abs(begin - end);
            repetition += twoPointDistance;
            sum += threePointDistance;
            last = twoPointDistance;

            if(contrast > optimizeDistance){
                optimizeDistance = contrast;
                select = threePointDistance;
                add = Math.abs(end-begin);
            }

        }
        System.out.println(sum - select + last - repetition + add);
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值