返回目录

题目描述

小明今年升学到了小学1年级来到新班级后,发现其他小朋友身高参差不齐,然后就想基于各小朋友和自己的身高差,对他们进行排序,请帮他实现排序。

输入描述

第一行为正整数 h和n,0<h<200 为小明的身高,0<n<50 为新班级其他小朋友个数。

第二行为n个正整数,h1 \~ hn分别是其他小朋友的身高,取值范围0<hi<200,且n个正整数各不相同。

输出描述

输出排序结果,各正整数以空格分割,

和小明身高差绝对值最小的小朋友排在前面,

和小明身高差绝对值最大的小朋友排在后面,

如果两个小朋友和小明身高差一样,则个子较小的小朋友排在前面。

示例:

输入100 10
输出95 96 97 98 99 101 102 103 104 105
说明小明身高100,班级学生10个,身高分别为95 96 97 98 99 101 102 103 104 105,按身高差排
序后结果为:99 101 98 102 97 103 96 104 95 105。

Python算法源码


def get_result(h, heights):
    # 根据到目标高度的距离和高度值进行排序
    heights.sort(key=lambda x: (abs(x - h), x))
    # 将排序后的高度数组转换为字符串并用空格连接
    return ' '.join(map(str, heights))

if __name__ == "__main__":
    # 读取输入的目标高度和高度数量
    h, n = map(int, input().split())
    # 读取输入的高度数组
    heights = list(map(int, input().split()))
    # 获取结果并打印
    result = get_result(h, heights)
    print(result)

C算法源码

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

// 结构体用于保存身高和与小明身高差的绝对值
struct HeightDifference {
    int height;
    int diff;
};

// 比较函数,用于排序
int compare(const void *a, const void *b) {
    struct HeightDifference *heightDiffA = (struct HeightDifference *)a;
    struct HeightDifference *heightDiffB = (struct HeightDifference *)b;
    // 如果差的绝对值相同,则身高较小的排在前面
    if (heightDiffA->diff == heightDiffB->diff) {
        return heightDiffA->height - heightDiffB->height;
    }
    // 否则,差的绝对值较小的排在前面
    return heightDiffA->diff - heightDiffB->diff;
}

int main() {
    int h, n;
    printf("Enter height of Xiao Ming and number of other classmates: ");
    scanf("%d %d", &h, &n);
    struct HeightDifference *heights = (struct HeightDifference *)malloc(n * sizeof(struct HeightDifference));
    printf("Enter heights of other classmates: ");
    for (int i = 0; i < n; i++) {
        scanf("%d", &heights[i].height);
        heights[i].diff = abs(heights[i].height - h);
    }
    // 使用快速排序对身高进行排序,使用自定义比较函数
    qsort(heights, n, sizeof(struct HeightDifference), compare);
    printf("Sorted heights: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", heights[i].height);
    }
    printf("\n");
    free(heights);
    return 0;
}

Java算法源码

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int h, n; 
        h = sc.nextInt();
        n = sc.nextInt();
        ArrayList<Integer> heights = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            int height = sc.nextInt();
            heights.add(height);
        }
        Collections.sort(heights, new Comparator<Integer>() {
            public int compare(Integer a, Integer b) {
                int diff_a = Math.abs(a - h);
                int diff_b = Math.abs(b - h);
                if (diff_a == diff_b) {
                    return a - b;
                }
                return diff_a - diff_b;
            }
        });
        for (int i = 0; i < n; i++) {
            System.out.print(heights.get(i) + " ");
        }
        System.out.println();
    }
}
最后修改:2024 年 04 月 02 日
如果觉得我的文章对你有用,请随意赞赏