机试题目一

1、找朋友

package Q2;
import java.util.Arrays;
import java.util.Scanner;

/**
 * 找朋友
 * 在学校中,N个小朋友站成一排,第i个小朋友的身高为height[i],
 第i个小朋友可以看到的第一个比自己高的小朋友j就是i的好朋友,要求 j > i,
 请重新生成一个列表,对应位置输出的是每个小朋友的好朋友的位置,没有则用0代替
 * 测试用例:
 * 输入:
 * 8
 * 123 124 125 121 119 122 126 123
 * 输出:
 * 1 2 6 5 5 6 0 0
 */
public class Num1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();   //人数
        int[] child = new int[n];
        for(int i = 0;i<n;i++){
            child[i] = sc.nextInt();
        }
        int[] friend =findFriend(child);
        for (int i =0 ;i<n;i++){
            if(i == n-1){
                System.out.print(friend[i]);
            }else{
                System.out.print(friend[i] + " ");
            }
        }
    }

    public static int[] findFriend(int[] nums){
        int[] result = new int[nums.length];
        if (nums.length == 0 || nums.length == 1){
            return result;
        }else{
            boolean isFind = false;
            int i =0;
            int j =1;
            while (i < nums.length && j<nums.length){
                if(nums[j] > nums[i]){
                    result[i] = j;
                    i = i+1;
                    j = i+1;
                    isFind = true;
                }else{
                    j++;
                    isFind = false;
                }
                if (!isFind) {
                   result[i] = 0;
                }
            }
        }
        return result;
    }
}

17、求众数以及中位数

import java.util.*;
/**
 * 【题目描述】
 * 1.众数是指一组数据中出现次数量多的那个数,众数可以是多个
 * 2.中位数是指把一组数据从小到大排列,最中间的那个数,如果这组数据的个数是奇数,那最中间那个就是中位数,如果这组数据的个数为偶数,
 *   那就把中间的两个数之和除以2,所得的结果就是中位数
 * 3.查找整型数组中元素的众数并组成一个新的数组,求新数组的中位数
 * 版权声明:本文为CSDN博主「小朱小朱绝不服输」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
 * 原文链接:https://blog.csdn.net/weixin_44052055/article/details/124749478
 * 【思路分析】
 * 1、先求众数数组,先统计每个数字出现的次数,统计出现最多次的数字,可以使用hashMap统计并排序。
 * 2、求众数数组中的中位数
 * 【测试数组】
 * 输入:10 11 21 19 21 17 21 16 21 18 15
 * 输出:21
 */
public class Num17 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String[] str = in.nextLine().split(" ");
        int[] nums = new int[str.length];
        for (int i = 0; i < str.length; i++) {
            nums[i] = Integer.parseInt(str[i]);
        }
        // hashmap统计出现的次数
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            map.put(nums[i], map.getOrDefault(nums[i], 0) + 1);
        }
        // 按出现次数排序
        List<Map.Entry<Integer,Integer>> list = new ArrayList<>(map.entrySet());
        list.sort((o1, o2) -> o2.getValue() - o1.getValue());
        int max = 0;
        // 获取众数列表
        List<Integer> mode = new ArrayList<>();
        for (Map.Entry<Integer, Integer> entry : list) {
            if (entry.getValue() >= max) {
                mode.add(entry.getKey());
                max = entry.getValue();
            } else {
                break;
            }
        }
        //5、求中位数
        int length = mode.size();
        if(length % 2 == 1){   //数据个数为奇数
            System.out.println( mode.get(length/2));
        }else{  //数据个数为偶数,中间两个数的平均数
            System.out.println((mode.get(length/2) + mode.get(length/2 -1))/2);
        }
    }
}

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值