分值: 100分题解: Java / Python / C++题目描述存储阵列上使用的一批固态硬盘,根据硬盘磨损值给定一个数组endurances,数组中每个元素表示单块硬盘的磨损度(0到10000之间)。磨损度越大,表示此盘需要更换的概率越高。需要找出磨损度最高三块盘下标和磨损度最低的三块盘下标。输入描述一组硬盘磨损度的数组。说明:(1) 数组endurances中无重复值(2) 数组的长度范围:[6,200](3) 数组的下标从0开始。输出描述第一行:磨损度最高三块盘下标,按下标升序展示第二行:磨损度最低的三块盘下标,按下标升序展示示例1输入:1 50 40 68 72 86 35 14 87 99 63 75输出:5 8 90 6 7解释:输入:1 50 40 68 72 86 35 14 87 99 63 75:表示一组硬盘磨损度的数组,其中的每个数值表示每个硬盘对应的磨损度。输出:5 8 9:表示磨损度最高三块盘的下标0 6 7:表示磨损度最低的三块盘的下标示例2输入:23 34 56 12 11 10输出:0 1 23 4 5解释:输入:23 34 56 12 11 10:表示一组硬盘磨损度的数组,其中的每个数值表示每个硬盘对应的磨损度。输出:0 1 2:表示磨损度最高三块盘的下标3 4 5:表示磨损度最低的三块盘的下标题解简单构造题将“磨损度” 和 “下标” 封装在一起然后根据“磨损度”排序,前三个是磨损度最低的三块盘, 后三个是磨损度最高的三块盘。Javaimport java.util.ArrayList;import java.util.Arrays;import java.util.Collections;import java.util.List;import java.util.Scanner;/** * @author code5bug */public class Main {    // 打印输出    static void print(List<Integer> list) {        int n = list.size();        for (int i = 0; i < n; i++) {            if (i + 1 == n) {                System.out.println(list.get(i));            } else {                System.out.print(list.get(i) + " ");            }        }    }    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        List<int[]> endurances = new ArrayList<>();        int t, i = 0;        while (scanner.hasNextInt()) {            t = scanner.nextInt();            endurances.add(new int[]{t, i++});        }        Collections.sort(endurances, (a, b) -> Integer.compare(a[0], b[0]));        List<Integer> resultMax = new ArrayList<>();        List<Integer> resultMin = new ArrayList<>();        int n = endurances.size();        for (int j = 0; j < 3; j++) {            resultMax.add(endurances.get(n - 1 - j)[1]);            resultMin.add(endurances.get(j)[1]);        }        Collections.sort(resultMax);        Collections.sort(resultMin);        print(resultMax);        print(resultMin);    }}Pythonendurances = list(map(int, input().split()))items = sorted(zip(endurances, range(len(endurances))))result_max = sorted([item[1] for item in items[-3:]])result_min = sorted([item[1] for item in items[:3]])print(*result_max)print(*result_min)C++#include <iostream>#include <vector>#include <algorithm>using namespace std;// 打印输出void print(vector<int>& v) {    int n = v.size();    for(int i=0; i<n; i++) {        if(i + 1 == n) {            cout << v[i] << endl;        } else {            cout << v[i] << " ";        }    }}int main() {    vector<pair<int,int>> endurances;    int t = 0, i = 0;    while(cin >> t) {        endurances.push_back(make_pair(t, i++));    }    sort(endurances.begin(), endurances.end());    vector<int> result_max;    vector<int> result_min;    int n = endurances.size();    for(int i=0; i<3; i++) {        result_max.push_back(endurances[n - 1 - i].second);        result_min.push_back(endurances[i].second);    }    sort(result_max.begin(), result_max.end());    sort(result_min.begin(), result_min.end());    print(result_max);    print(result_min);    return 0;}🙏整理题解不易, 如果有帮助到您,请给点个赞 ‍❤️‍ 和收藏 ⭐,让更多的人看到。🙏🙏🙏
点赞 10
评论 3
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务