5.2 美的笔试 JAVA

第一题

用java编写代码。给定一个二维平面和一些点,找出距离最近的两个点之间的距离。

输入描述:第一行包含一个整数n,表示点的个数。接下来n行,每行包含两个实数xi和yi,表示一个点的坐标。

输出描述:输出一个实数,表示最近的两个点之间的距离,结果保留两位小数。

示例1:

输入:

4

0 0

0 1

1 0

1 1

输出:

1.00

import java.util.*;
class Point2{
    double x;
    double y;
    Point2(double x, double y){
        this.x = x;
        this.y = y;
    }
}
public class pointdistance {
    public double countdistance(double x1, double x2, double y1, double y2){
        return Math.sqrt(Math.pow(x1-x2, 2) + Math.pow(y1-y2, 2));
    }
    public double minDistance(Point2[] point2s, int n){
        double mindistance = Double.MAX_VALUE;
        for (int i = 0; i < n; i++){
            for (int j = i+1; j < n; j++){
                double dist = countdistance(point2s[i].x, point2s[j].x, point2s[i].y, point2s[j].y);
                mindistance = Math.min(mindistance, dist);
            }
        }
        return mindistance;
    }

    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        Point2[] point2s = new Point2[n];
        for (int i = 0; i < n; i++){
            double x = in.nextDouble();
            double y = in.nextDouble();
            point2s[i] = new Point2(x, y);
        }
        pointdistance obj = new pointdistance();
        double mindistance = obj.minDistance(point2s, n);
        System.out.printf("%.2f", mindistance);
    }
}

 

第二题

用java编写代码,付工一天可以吃一根冰棍,也可以吃两根冰棍。求付工吃完n根冰棍有多少种可能的天数。(例如吃完5根冰棍有8种可能的天数)

输入描述:一个正整数,冰棍数量n。

输出描述:正整数,可能的种类数量。

示例1:

输入:

2

输出:

2

 

import java.util.Scanner;

public class test7 {
    public static int countPossibleDays(int n) {
        // 初始化dp数组,dp[i]表示吃完i根冰棍有多少种可能的天数
        int[] dp = new int[n + 1];

        // 初始条件
        dp[0] = 1; // 没有冰棍时,算一种可能的天数
        dp[1] = 1; // 吃完一根冰棍,只有一种可能的天数

        // 递推求解dp数组
        for (int i = 2; i <= n; i++) {
            dp[i] = dp[i - 1] + dp[i - 2];
        }

        // 返回吃完n根冰棍的可能天数
        return dp[n];
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt(); // 吃完n根冰棍

        // 输出吃完n根冰棍的可能天数
        System.out.println(countPossibleDays(n));

        scanner.close();
    }
}

 

第三题

用java编写代码。

假设你有一些任务需要完成,每个任务都有一个开始时间和结束时间。你需要从这些任务中选择一些来完成,使得你能够完成尽可能多的任务,且每个任务之间不会产生时间冲突。具体来说,对于每个任务i,都有一个开始时间start[i]和结束时间end[i]。你需要选择一些任务,使得它们之间没有时间冲突(start[i] >end[j]),并且完成的任务数量最多。输出最大的任务数量。

输入描述:第一行包含一个整数n,表示任务数量。

接下来n行,每行包含两个整数a和b,表示每个任务的开始和结束时间。

输出描述:最大任务数量

示例1:

输入:

3

2 4

5 6

7 8

输出:

3

import java.util.*;

class Task2{
    int start;
    int end;
    Task2(int start, int end){
        this.start = start;
        this.end = end;
    }
}
public class maxtasks {
    public int maxTasks2(Task2[] tasks, int n){
        Arrays.sort(tasks, Comparator.comparingInt(a -> a.end)); // 按结束时间升序排序
        int count = 0;
        int prevEnd = 0;
        for (Task2 task : tasks){ // 如果当前任务的开始时间晚于上一个任务的结束时间,则可以选择该任务
            if (task.start >= prevEnd){
                count++;
                prevEnd = task.end; // 更新上一个任务的结束时间
            }
        }
        return count;
    }

    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        Task2[] task = new Task2[n];
        // 读取任务的开始和结束时间
        for (int i = 0; i < n; i++){
            int start = in.nextInt();
            int end = in.nextInt();
            task[i] = new Task2(start, end);
        }
        maxtasks obj = new maxtasks();
        System.out.println(obj.maxTasks2(task, n));
    }
}

只过了66%,还没找到原因。

 

 其他:练习层序遍历

import java.util.*;
public class ceng_xu {
    List<List<Integer>> resList = new ArrayList<List<Integer>>();

    public List<List<Integer>> levelorder(TreeNode root){
        levelorderfun(root, 0);
        return resList;
    }

    public void levelorderfun(TreeNode node, int deep){
        if (node == null) return;
        deep++;
        while (resList.size() < deep) resList.add(new ArrayList<Integer>());
        resList.get(deep-1).add(node.val);
        levelorderfun(node.left, deep);
        levelorderfun(node.right, deep);
    }

    public static void main(String[] args){
        TreeNode head = new TreeNode(5);
        head.left = new TreeNode(4);
        head.left.left = new TreeNode(1);
        head.left.right = new TreeNode(2);
        head.right = new TreeNode(6);
        head.right.left = new TreeNode(7);
        head.right.right = new TreeNode(8);
        ceng_xu obj = new ceng_xu();
        List<List<Integer>> resList2 = new ArrayList<List<Integer>>();
        resList2 = obj.levelorder(head);
        System.out.println(resList2);
    }
}

 

posted @ 2024-05-06 15:21  实数集  阅读(62)  评论(0)    收藏  举报